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
#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
#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
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]from0tos.length() - 1and 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
1. Which loop condition is typically used for index-based traversal of a `std::string s`?
2. What does `for (char c : s)` do?
3. What condition typically ends traversal of a C-style string using an index-based loop?
4. Which access method used during traversal performs bounds checking?
5. What is a key advantage of the range-based for loop over an index-based loop for simple character traversal?
Was this page helpful?
You May Also Like
String Functions in C++
Explore the most commonly used std::string member functions — length, substr, find, append, at, empty, and c_str — with practical examples.
for Loop in C++
Learn the C++ `for` loop syntax, how init/condition/increment control iteration, and when it is the best choice for a known number of repetitions.
Array of Characters in C++
Learn how a C-style string is a null-terminated char array in C++, how to declare and print one, and how it differs from the string class.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics