📖 Scenario: You are working on a simple text processing tool that needs to analyze a given word letter by letter.Imagine you have a word and you want to look at each letter to do some checks or transformations later.
🎯 Goal: Learn how to go through each character in a string one by one and access them using their position.You will write code to create a string, set up a counter, loop through the string characters, and finally print each character on its own line.
📋 What You'll Learn
Create a string variable with the exact value "HELLO"
Create an integer variable called index to track position
Use a while loop to traverse the string using index
Access each character using the string and index
Print each character on its own line
💡 Why This Matters
🌍 Real World
Text processing tools often need to look at each letter in a word to check spelling, count letters, or transform text.
💼 Career
Understanding how to traverse strings and access characters is a basic skill for programming jobs involving text manipulation, such as software development, data processing, and scripting.
Progress0 / 4 steps
1
Create the string variable
Create a string variable called word and set it to the exact value "HELLO".
DSA C
Hint
Use char word[] = "HELLO"; to create the string.
2
Create the index variable
Create an integer variable called index and set it to 0.
DSA C
Hint
Use int index = 0; to start from the first character.
3
Traverse the string using a while loop
Write a while loop that continues as long as word[index] is not the null character '\0'. Inside the loop, print the character at word[index] and then increase index by 1.
DSA C
Hint
Use while (word[index] != '\0') to loop through each character.
Print each character with printf("%c\n", word[index]).
Increase index by 1 inside the loop.
4
Print the characters
Run the program to print each character of word on its own line.
DSA C
Hint
Make sure your program prints each letter on a new line exactly as shown.
Practice
(1/5)
1. What does the expression str[0] represent when str is a string in C?
easy
A. The first character of the string
B. The length of the string
C. The last character of the string
D. A pointer to the string
Solution
Step 1: Understand string indexing in C
In C, strings are arrays of characters. Indexing starts at 0, so str[0] accesses the first character.
Step 2: Identify what str[0] returns
It returns the character stored at the first position of the string, not the length or pointer.
Final Answer:
The first character of the string -> Option A
Quick Check:
str[0] = first character [OK]
Hint: Remember: string indexing starts at zero in C [OK]
Common Mistakes:
Confusing character access with string length
Thinking indexing starts at 1
Mixing pointer and character access
2. Which of the following is the correct way to traverse a string str of length n in C using a for loop?
easy
A. for (int i = 1; i < n; i++)
B. for (int i = 0; i <= n; i++)
C. for (int i = 0; i < n; i++)
D. for (int i = 1; i <= n; i++)
Solution
Step 1: Understand string indexing and loop bounds
String indices in C start at 0 and go up to n-1 for length n.
Step 2: Choose loop condition to cover all characters
The loop should run from 0 to n-1, so i < n is correct.
Final Answer:
for (int i = 0; i < n; i++) -> Option C
Quick Check:
Loop from 0 to n-1 = for (i=0; i<n; i++) [OK]
Hint: Loop from 0 to less than length for strings [OK]
Common Mistakes:
Using <= n causes out-of-bounds access
Starting loop at 1 misses first character
Confusing string length with last index
3. What is the output of the following C code?
char str[] = "hello";
for (int i = 0; i < 5; i++) {
printf("%c-", str[i]);
}
medium
A. hello-
B. h-e-l-l-o-
C. h e l l o
D. h-e-l-l-o
Solution
Step 1: Analyze the loop and print statement
The loop runs from i=0 to 4, printing each character followed by a dash.
Hint: Look at printf format and loop range carefully [OK]
Common Mistakes:
Ignoring the dash after each character
Printing the whole string once instead of chars
Confusing spaces with dashes
4. Identify the error in the following code snippet that tries to print each character of a string str.
char str[] = "world";
for (int i = 1; i <= 5; i++) {
printf("%c", str[i]);
}
medium
A. Both A and B are errors
B. Loop condition should be i < 5 instead of <= 5
C. No error, code works fine
D. Loop starts at 1 and misses the first character
Solution
Step 1: Check loop start index
Loop starts at 1, so it skips str[0] which is the first character.
Step 2: Check loop end condition
Loop runs while i <= 5, but valid indices are 0 to 4. Accessing str[5] causes out-of-bounds error.
Final Answer:
Both A and B are errors -> Option A
Quick Check:
Start at 0 and use i < length to avoid errors [OK]
Hint: Loop from 0 to less than length to avoid errors [OK]
Common Mistakes:
Starting loop at 1 and missing first char
Using <= length causing out-of-bounds access
Ignoring array indexing rules
5. Given a string str of unknown length, which of the following code snippets correctly prints all characters until the null terminator in C?
hard
A. for (int i = 0; str[i] != '\0'; i++) { printf("%c", str[i]); }
B. for (int i = 0; i < strlen(str); i++) { printf("%c", str[i]); }
C. while (*str != '\0') { printf("%c", *str); str++; }
D. All of the above
Solution
Step 1: Analyze for (int i = 0; str[i] != '\0'; i++) { printf("%c", str[i]); }
Loops until character is not null terminator, printing each character. Correct method.
Step 2: Analyze for (int i = 0; i < strlen(str); i++) { printf("%c", str[i]); } and C
for (int i = 0; i < strlen(str); i++) { printf("%c", str[i]); } uses strlen to get length, then loops printing characters. while (*str != '\0') { printf("%c", *str); str++; } uses pointer traversal until null terminator. Both are correct.
Step 3: Combine results
All three methods correctly print the string characters until the null terminator.
Final Answer:
All of the above -> Option D
Quick Check:
All methods stop at '\0' and print chars [OK]
Hint: Any loop stopping at '\0' prints full string safely [OK]
Common Mistakes:
Not checking for null terminator causing overflow
Using wrong loop conditions
Modifying string pointer incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.