📖 Scenario: You work at a company that processes customer feedback messages. Each message is a string, and you need to analyze the characters inside it.
🎯 Goal: Build a simple program that goes through a given feedback message string, accesses each character one by one, and prints them out separately.
📋 What You'll Learn
Create a string variable with a specific feedback message
Create a variable to hold the length of the string
Use a for loop to traverse the string using the length variable
Access each character by its index inside the loop
Print each character on a new line
💡 Why This Matters
🌍 Real World
Processing and analyzing text messages, feedback, or any user input often requires going through each character to check or transform data.
💼 Career
Understanding string traversal is fundamental for roles in software development, data processing, and quality assurance where text manipulation is common.
Progress0 / 4 steps
1
Create the feedback message string
Create a string variable called feedback and set it to the exact value "Great product!"
DSA C#
Hint
Use string feedback = "Great product!"; to create the string variable.
2
Create a variable for the string length
Create an integer variable called length and set it to the length of the string feedback using feedback.Length
DSA C#
Hint
Use int length = feedback.Length; to get the string length.
3
Traverse the string using a for loop
Write a for loop with an integer variable i starting at 0, running while i < length, and increasing i by 1 each time
DSA C#
Hint
Use for (int i = 0; i < length; i++) to loop through the string indices.
4
Print each character in the string
Inside the for loop, print the character at index i of feedback using Console.WriteLine(feedback[i])
DSA C#
Hint
Use Console.WriteLine(feedback[i]); inside the loop to print each character on its own line.
Practice
(1/5)
1. What does the expression str[2] return when string str = "hello";?
easy
A. The character 'o'
B. The character 'l'
C. The character 'e'
D. An error because strings cannot be indexed
Solution
Step 1: Understand zero-based indexing in strings
In C#, string characters are accessed starting from index 0. So, str[0] = 'h', str[1] = 'e', str[2] = 'l'.
Step 2: Identify the character at index 2
The character at index 2 in "hello" is 'l'.
Final Answer:
The character 'l' -> Option B
Quick Check:
str[2] = 'l' [OK]
Hint: Remember string indexing starts at 0 in C# [OK]
Common Mistakes:
Thinking indexing starts at 1
Confusing characters at index 1 and 2
Assuming strings can't be indexed
2. Which of the following is the correct way to loop through each character in a string str in C#?
easy
A. for (char c = 0; c < str.Length; c++) { Console.WriteLine(str[c]); }
B. for (int i = 1; i <= str.Length; i++) { Console.WriteLine(str[i]); }
C. foreach (int c in str) { Console.WriteLine(c); }
D. for (int i = 0; i < str.Length; i++) { Console.WriteLine(str[i]); }
Solution
Step 1: Check loop indexing and range
In C#, string indexing starts at 0 and goes up to Length - 1. So the loop must run from 0 to str.Length - 1.
Step 2: Validate each option
for (int i = 0; i < str.Length; i++) { Console.WriteLine(str[i]); } uses correct indexing (0 to Length-1). for (int i = 1; i <= str.Length; i++) { Console.WriteLine(str[i]); } starts at 1 and uses <= which causes out-of-range error. foreach (int c in str) { Console.WriteLine(c); } tries to foreach over int, but string yields chars, so type mismatch. for (char c = 0; c < str.Length; c++) { Console.WriteLine(str[c]); } uses char as loop variable which is incorrect.
Final Answer:
for (int i = 0; i < str.Length; i++) { Console.WriteLine(str[i]); } -> Option D
Quick Check:
Loop from 0 to Length-1 for string traversal [OK]
Hint: Use for loop with i from 0 to str.Length - 1 [OK]
Common Mistakes:
Starting loop at 1 instead of 0
Using <= instead of < in loop condition
Using wrong variable type in foreach
3. What will be the output of the following code?
string s = "code";
for (int i = 0; i < s.Length; i++)
{
if (i % 2 == 0)
Console.Write(s[i]);
}
medium
A. co
B. oe
C. cd
D. code
Solution
Step 1: Understand the loop and condition
The loop runs from i=0 to i=3 (length 4). It prints characters only when i is even (0, 2).
Step 2: Identify characters at even indices
At i=0, s[0] = 'c'; at i=2, s[2] = 'd'. So output is "cd".
Final Answer:
cd -> Option C
Quick Check:
Print chars at even indices: 'c' and 'd' [OK]
Hint: Print characters where index % 2 == 0 [OK]
Common Mistakes:
Confusing even and odd indices
Printing all characters ignoring condition
Misreading string length
4. Identify the error in the following code snippet:
string text = "example";
for (int i = 0; i <= text.Length; i++)
{
Console.Write(text[i]);
}
medium
A. The loop condition should be < instead of <=
B. The string cannot be indexed with []
C. Variable i should be of type char
D. Console.Write should be Console.WriteLine
Solution
Step 1: Analyze loop condition and indexing
The loop runs from i=0 to i <= text.Length. Since string indices go from 0 to Length-1, accessing text[Length] causes an error.
Step 2: Correct the loop condition
Change condition to i < text.Length to avoid out-of-range exception.
Final Answer:
The loop condition should be < instead of <= -> Option A
Quick Check:
Loop must run from 0 to Length-1 [OK]
Hint: Use i < str.Length to avoid index out of range [OK]
Common Mistakes:
Using <= instead of < in loop condition
Assuming string indexing is 1-based
Changing variable type unnecessarily
5. Given a string input, which code snippet correctly counts how many vowels (a, e, i, o, u) it contains?
hard
A. int count = 0;
foreach (char c in input.ToLower()) {
if ("aeiou".Contains(c)) count++;
}
Console.WriteLine(count);
B. int count = 0;
for (int i = 1; i <= input.Length; i++) {
if ("aeiou".Contains(input[i])) count++;
}
Console.WriteLine(count);
C. int count = 0;
foreach (int c in input) {
if ("aeiou".Contains((char)c)) count++;
}
Console.WriteLine(count);
D. int count = 0;
for (char c = 0; c < input.Length; c++) {
if ("aeiou".Contains(input[c])) count++;
}
Console.WriteLine(count);
Solution
Step 1: Check correct iteration over string characters
int count = 0;
foreach (char c in input.ToLower()) {
if ("aeiou".Contains(c)) count++;
}
Console.WriteLine(count); uses foreach over chars in input.ToLower(), which is correct. int count = 0;
for (int i = 1; i <= input.Length; i++) {
if ("aeiou".Contains(input[i])) count++;
}
Console.WriteLine(count); uses 1-based indexing causing out-of-range error. int count = 0;
foreach (int c in input) {
if ("aeiou".Contains((char)c)) count++;
}
Console.WriteLine(count); uses foreach over int which is incorrect type. int count = 0;
for (char c = 0; c < input.Length; c++) {
if ("aeiou".Contains(input[c])) count++;
}
Console.WriteLine(count); uses char as loop variable which is invalid.
Step 2: Verify vowel check and counting
int count = 0;
foreach (char c in input.ToLower()) {
if ("aeiou".Contains(c)) count++;
}
Console.WriteLine(count); checks if character is in "aeiou" string and increments count correctly.
Final Answer:
Option A code snippet correctly counts vowels -> Option A
Quick Check:
Use foreach char and check vowels with Contains [OK]
Hint: Use foreach char in lowercase string and Contains("aeiou") [OK]
Common Mistakes:
Using wrong loop indices causing errors
Incorrect variable types in loops
Not converting string to lowercase before checking
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.