DSA FUNDAMENTALS › STRING BASICS
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.
| CHARACTER | ASCII VALUE |
|---|---|
| 'A' | 65 |
| '7' | 55 |
| space | 32 |
| '!' | 33 |
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.
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.
| INDEX | CHARACTER | ASCII VALUE |
|---|---|---|
| 0 | 'A' | 65 |
| 1 | '7' | 55 |
| 2 | space | 32 |
| 3 | '!' | 33 |
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.
i = 0 -> 'A'
i = 1 -> '7'
i = 2 -> space
i = 3 -> '!'
i = 4 -> stopCharacter 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.
static_cast<int>(text[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.
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.
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.
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.
}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.
}character='A', ASCII=65
character='7', ASCII=55
character=<space>, ASCII=32
character='!', ASCII=33The 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.