100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

String Traversal in C++

Learn how to iterate through every character of a C++ string using index-based for loops and the range-based for loop, with practical examples.

StringsBeginner6 min readJul 7, 2026
Analogies

1. Intro

String traversal means visiting each character of a string one by one, typically to inspect, count, or transform them. In C++, both std::string and C-style strings support traversal through index-based loops using [], and std::string additionally supports the modern range-based for loop, which is simpler and less error-prone.

🏏

Cricket analogy: Reading a scorecard ball-by-ball with a counter from ball 1 to the last ball is index-based traversal, while simply reading each entry aloud in order without tracking the ball number is like the range-based for loop over a string.

2. Syntax

cpp
#include <string>
using namespace std;

string s = "Hello";

// index-based traversal
for (int i = 0; i < s.length(); i++) {
    char c = s[i];
}

// range-based for loop
for (char c : s) {
    // use c
}

3. Explanation

The index-based approach uses a counter i from 0 to s.length() - 1 and accesses each character with s[i] (or the bounds-checked s.at(i)). This gives you the index of each character, which is useful when position matters. The range-based for loop for (char c : s) automatically walks through every character in order without needing an explicit index — it is cleaner when you only care about the character values themselves, not their positions. Both approaches work for std::string; C-style char arrays typically use index-based traversal combined with checking for the null terminator '\0' as the loop's stopping condition.

🏏

Cricket analogy: Looping for (int i = 0; i < name.length(); i++) over "Kohli" and reading name[i] or the bounds-checked name.at(i) gives you each letter's position, useful for finding initials, while for (char c : name) simply visits each letter without tracking index, and a C-style name array would instead loop until hitting '\0'.

4. Example

cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "Hello";

    cout << "Index-based: ";
    for (int i = 0; i < s.length(); i++) {
        cout << s[i] << "-";
    }
    cout << endl;

    cout << "Range-based: ";
    for (char c : s) {
        cout << c << "-";
    }
    cout << endl;
    return 0;
}

5. Output

text
Index-based: H-e-l-l-o-
Range-based: H-e-l-l-o-

6. Key Takeaways

  • String traversal visits each character of a string, usually via a loop.
  • Index-based loops use s[i] from 0 to s.length() - 1 and give access to the position.
  • The range-based for loop for (char c : s) is a simpler way to visit every character in order.
  • C-style strings are traversed similarly but often stop when the null terminator '\0' is reached.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#StringTraversalInC#String#Traversal#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep