DSA SheetLesson · no judge

DSA FUNDAMENTALSSTRING BASICS

Print Each Character's ASCII Value

Reading · 7 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 13

A character and its ASCII value are two views of the same stored item

The string "A7 !" has four characters. Each character has an ASCII value, which is the integer used to represent that character: 'A' is 65, '7' is 55, the space is 32, and '!' is 33. The character '7' is not the same thing as the numeric value 7. '7' is a character that represents the digit, while 7 is an integer, and their values are different.

CHARACTERASCII VALUE
'A'65
'7'55
space32
'!'33
The four characters and their ASCII values
the four indexed characters of "A7 !" aligned with their ASCII valuesA three-row alignment for the string "A7 !". Index 0 contains 'A' and maps to ASCII 65. Index 1 contains '7' and maps to ASCII 55. Index 2 contains a space and maps to ASCII 32. Index 3 contains '!' and maps to ASCII 33.0123'A''7'' ' (space)'!'65553233indexcharacterASCIIEach position stores one character; each character maps to one distinct ASCIIinteger.
The character '7' maps to 55, and the space maps to 32.
CHECKPOINT 1Not answered

What is the ASCII value of the character '7' in "A7 !"?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

An index-based loop reaches the space just like every visible character

String indices begin at 0, so the characters in "A7 !" are reached at indices 0, 1, 2, and 3. The space is at index 2, even though it does not look like a visible mark. It is still a character stored in the string and must be processed like every other character.

INDEXCHARACTERASCII VALUE
0'A'65
1'7'55
2space32
3'!'33
The loop's four visits

A loop with the condition i < text.length() visits index 0, then 1, then 2, then 3. After the update from 3 to 4, the condition becomes 4 < 4, which is false, so the loop stops. Index 4 is excluded because it is one position past the last valid index.

TEXTThe loop visits every valid index, including the space at index 2.
i = 0  ->  'A'
i = 1  ->  '7'
i = 2  ->  space
i = 3  ->  '!'
i = 4  ->  stop

Printing a code requires an explicit numeric conversion

Character access gives you the character itself. If you print text[i] in C++ or text.charAt(i) in Java, the output is the character, not its ASCII value. To display the numeric code, convert that character to an integer before printing it.

CPPC++ numeric conversion for the character at index i.
static_cast<int>(text[i])
JAVAJava numeric conversion for the character at index i.
(int) text.charAt(i)

The conversion changes how the value is displayed, not the character stored in "A7 !". At index 1, the stored character is still '7'. Directly printing it displays 7 as a character, while printing its converted value displays 55 as an integer.

CHECKPOINT 2Not answered

Fill in the numeric-conversion expression for the character at index i in your selected language.

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

One loop iteration must produce one character-code pair

Each iteration needs three actions: access the character at the current index, convert it to an integer, and print both values together. Since a space is hard to see, give it an explicit label in the output instead of leaving a blank-looking gap.

CPPThe C++ traversal and conversion for one pair per iteration.
for (int i = 0; i < text.length(); i++) {
    char c = text[i];
    int code = static_cast<int>(c);
    // Print c, or a visible label when c is a space, together with code.
}
JAVAThe Java traversal and conversion for one pair per iteration.
for (int i = 0; i < text.length(); i++) {
    char c = text.charAt(i);
    int code = (int) c;
    // Print c, or a visible label when c is a space, together with code.
}
TEXTThe exact ordered output for "A7 !", with the space shown as <space>.
character='A', ASCII=65
character='7', ASCII=55
character=<space>, ASCII=32
character='!', ASCII=33

The four iterations preserve the string's order: index 0 produces 'A' with 65, index 1 produces '7' with 55, index 2 produces the labeled space with 32, and index 3 produces '!' with 33. No character is skipped just because it is difficult to see.

Next part · Quiz