📖 Scenario: Imagine you are creating a simple program that reads a word and then looks at each letter one by one. This is like reading a word slowly to see each letter clearly.
🎯 Goal: You will build a program that stores a word, sets up a way to count letters, goes through each letter one by one, and finally prints all the letters separated by spaces.
📋 What You'll Learn
Create a string variable with a specific word
Create an integer variable to count letters
Use a for loop to go through each character in the string
Print all characters separated by spaces
💡 Why This Matters
🌍 Real World
Reading and processing words letter by letter is useful in text analysis, games like word puzzles, and input validation.
💼 Career
Understanding string traversal is fundamental for software developers working with text processing, user input handling, and data parsing.
Progress0 / 4 steps
1
Create the word string
Create a string variable called word and set it to the exact value "hello".
DSA C++
Hint
Use std::string to create the variable and assign the word "hello" to it.
2
Set up a counter for letters
Create an integer variable called length and set it to the length of the string word using word.length().
DSA C++
Hint
Use word.length() to find how many letters are in the word.
3
Loop through each character
Use a for loop with an integer variable i starting at 0 and running while i < length. Inside the loop, access each character of word using word[i].
DSA C++
Hint
Use for (int i = 0; i < length; i++) and inside the loop get the letter with word[i].
4
Print all letters separated by spaces
Inside the for loop, print each character word[i] followed by a space using std::cout. After the loop, print a newline with std::cout << std::endl;.
DSA C++
Hint
Use std::cout << letter << ' '; inside the loop and std::cout << std::endl; after the loop.
Practice
(1/5)
1. What does the expression str[i] do when str is a string in C++?
easy
A. Returns the length of the string
B. Accesses the character at index i in the string
C. Deletes the character at index i
D. Converts the string to uppercase
Solution
Step 1: Understand string indexing in C++
In C++, strings are like arrays of characters. Using str[i] accesses the character at position i.
Step 2: Clarify what str[i] returns
The expression returns the character stored at index i without modifying the string.
Final Answer:
Accesses the character at index i in the string -> Option B
Quick Check:
str[i] = character at i [OK]
Hint: Remember strings are arrays of chars; indexing gets a character [OK]
Common Mistakes:
Thinking it returns string length
Assuming it deletes characters
Confusing with string case conversion
2. Which of the following is the correct way to loop through each character of a string str in C++?
easy
A. for (int i = 0; i <= str.length(); i++)
B. for (int i = 1; i < str.length(); i++)
C. for (int i = 0; i < str.length(); i++)
D. for (int i = 1; i <= str.length(); i++)
Solution
Step 1: Understand string length and indexing
String indices start at 0 and go up to str.length() - 1. So loop must run while i < str.length().
Step 2: Check loop conditions
for (int i = 0; i < str.length(); i++) uses i < str.length() starting from 0, which correctly covers all characters without going out of bounds.
Final Answer:
for (int i = 0; i < str.length(); i++) -> Option C
Quick Check:
Loop from 0 to length-1 [OK]
Hint: Loop from 0 to less than length to avoid out-of-bounds [OK]
Common Mistakes:
Using <= causes out-of-bounds error
Starting loop at 1 misses first character
Confusing length with last index
3. What is the output of the following C++ code?
std::string s = "hello";
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'l') {
std::cout << i << " ";
}
}
medium
A. 0 1
B. 1 2
C. 3 4
D. 2 3
Solution
Step 1: Identify characters and their indices
The string "hello" has characters: h(0), e(1), l(2), l(3), o(4).
Step 2: Check which indices have 'l'
Characters at indices 2 and 3 are 'l', so those indices are printed.
Final Answer:
2 3 -> Option D
Quick Check:
Indices of 'l' = 2 3 [OK]
Hint: Check string indices carefully for matching characters [OK]
Common Mistakes:
Confusing character positions
Printing characters instead of indices
Off-by-one errors in indexing
4. Identify the error in this code snippet that tries to print all characters of a string str:
for (int i = 0; i <= str.length(); i++) {
std::cout << str[i];
}
medium
A. Using <= causes out-of-bounds access
B. Missing semicolon after for loop
C. str[i] cannot be used to access characters
D. Loop variable should be unsigned
Solution
Step 1: Analyze loop condition
The loop runs while i <= str.length(), which means it tries to access str[str.length()].
Step 2: Understand string indexing limits
Valid indices are from 0 to str.length() - 1. Accessing str[str.length()] is out-of-bounds and causes undefined behavior.
Final Answer:
Using <= causes out-of-bounds access -> Option A
Quick Check:
Loop must use <, not <= [OK]
Hint: Use < not <= to avoid accessing past string end [OK]
Common Mistakes:
Using <= instead of < in loop condition
Assuming string length is last index
Ignoring out-of-bounds errors
5. Given a string str, how can you create a new string containing only the vowels from str using traversal and character access in C++?
hard
A. Loop through str, check if each character is a vowel, and append it to a new string
B. Use str.substr() to extract vowels directly
C. Replace all consonants with spaces in str
D. Sort the string and pick vowels from the start
Solution
Step 1: Traverse the string character by character
Use a loop to access each character in str using indexing.
Step 2: Check if character is a vowel and build new string
If the character is one of 'a', 'e', 'i', 'o', 'u' (case-insensitive), append it to a new string variable.
Step 3: Return or print the new string
After the loop, the new string contains only vowels from the original string.
Final Answer:
Loop through str, check if each character is a vowel, and append it to a new string -> Option A
Quick Check:
Filter vowels by checking each character [OK]
Hint: Check each char; add vowels to new string [OK]
Common Mistakes:
Trying to extract vowels without looping
Using substr incorrectly
Sorting string changes order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.