Bird
Raised Fist0
DSA C#programming~10 mins

String Traversal and Character Access in DSA C# - Execution Trace

Choose your learning style10 modes available
Concept Flow - String Traversal and Character Access
Start at index = 0
↓
Check: index < string length?
No→END
Yes↓
Access character at index
↓
Process character (e.g., print)
↓
Increment index by 1
↩Back to Check
This flow shows how we start from the first character of a string, check if we are still inside the string, access the character, process it, then move to the next character until the end.
Execution Sample
DSA C#
string text = "hello";
for (int i = 0; i < text.Length; i++)
{
    Console.Write(text[i]);
}
This code goes through each character in the string "hello" and prints it one by one.
Execution Table
Stepi (index)Condition i < LengthCharacter AccessedActionOutput So Far
100 < 5 = Truetext[0] = 'h'Print 'h'h
211 < 5 = Truetext[1] = 'e'Print 'e'he
322 < 5 = Truetext[2] = 'l'Print 'l'hel
433 < 5 = Truetext[3] = 'l'Print 'l'hell
544 < 5 = Truetext[4] = 'o'Print 'o'hello
655 < 5 = FalseNo AccessExit loophello
💡 Loop stops because index 5 is not less than string length 5.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
i0 (initial)123455
Output"""h""he""hel""hell""hello""hello"
Key Moments - 3 Insights
Why does the loop stop when i equals 5 even though the string has 5 characters?
The loop condition checks if i is less than the string length (5). When i is 5, 5 < 5 is false, so the loop stops before accessing text[5], which would be out of bounds. See execution_table step 6.
What does text[i] mean inside the loop?
text[i] accesses the character at position i in the string. For example, at i=0, text[0] is 'h'. This is shown in execution_table steps 1 to 5.
Why do we start i at 0 instead of 1?
String indexing starts at 0 in C#, so the first character is at index 0. Starting at 0 ensures we access all characters from the start. See variable_tracker for i starting at 0.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what character is accessed at step 3?
A'e'
B'l'
C'o'
D'h'
💡 Hint
Check the 'Character Accessed' column at step 3 in the execution_table.
At which step does the loop condition become false?
AStep 5
BStep 4
CStep 6
DStep 1
💡 Hint
Look at the 'Condition i < Length' column to find when it becomes false.
If the string was "hi" instead of "hello", how many times would the loop run?
A2 times
B3 times
C5 times
D1 time
💡 Hint
Refer to variable_tracker for how 'i' changes and relate it to string length.
Concept Snapshot
String Traversal and Character Access in C#:
- Use a for loop: for(int i=0; i < str.Length; i++)
- Access characters by str[i]
- Indexing starts at 0
- Loop stops when i equals string length
- Common to print or process each character inside the loop
Full Transcript
This lesson shows how to go through each character in a string using a for loop in C#. We start with index i at 0, check if i is less than the string length, access the character at position i, do something with it like print, then increase i by 1. We repeat until i is no longer less than the string length. The loop stops before accessing out-of-range characters. This method helps process strings one character at a time safely and clearly.

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

  1. 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'.
  2. Step 2: Identify the character at index 2

    The character at index 2 in "hello" is 'l'.
  3. Final Answer:

    The character 'l' -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    for (int i = 0; i < str.Length; i++) { Console.WriteLine(str[i]); } -> Option D
  4. 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

  1. 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).
  2. Step 2: Identify characters at even indices

    At i=0, s[0] = 'c'; at i=2, s[2] = 'd'. So output is "cd".
  3. Final Answer:

    cd -> Option C
  4. 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

  1. 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.
  2. Step 2: Correct the loop condition

    Change condition to i < text.Length to avoid out-of-range exception.
  3. Final Answer:

    The loop condition should be < instead of <= -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Option A code snippet correctly counts vowels -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard