Recall & Review
beginner Click to reveal answer
What does string traversal mean?
String traversal means going through each character in a string one by one, usually from start to end.
beginner Click to reveal answer
How do you access the third character of a string named
str in C++?You use
str[2] because indexing starts at 0, so the third character is at position 2.intermediate Click to reveal answer
What happens if you try to access a character outside the string length in C++?
Accessing outside the string length causes undefined behavior, which can lead to errors or crashes.
beginner Click to reveal answer
Show a simple C++ loop to print all characters of a string
str.for (int i = 0; i < str.size(); i++) { std::cout << str[i]; }
beginner Click to reveal answer
Why is it important to use
str.size() or str.length() in loops?Using
str.size() or str.length() ensures you only access valid characters and avoid errors.What is the index of the first character in a C++ string?
✗ Incorrect
In C++, string indexing starts at 0, so the first character is at index 0.
Which of these is the correct way to get the length of a string
str in C++?✗ Incorrect
Both
str.length() and str.size() return the length of the string.What will happen if you access
str[str.size()]?✗ Incorrect
Accessing index equal to string size is outside bounds and causes undefined behavior.
Which loop correctly traverses all characters of a string
str?✗ Incorrect
Loop from 0 to less than size covers all valid indices.
How do you print the second character of a string
str?✗ Incorrect
Index 1 is the second character because indexing starts at 0.
Explain how to traverse a string in C++ and print each character.
Think about using a for loop and the string's size() method.
You got /3 concepts.
Describe what happens if you try to access a character beyond the last index of a string.
Consider what happens when you go outside the string length.
You got /3 concepts.
