C++20 sections · 1024 units
Open in Course

Iterating Over Strings

Processing each character

Loop through characters with index-based for. Write for (int i = 0; i < s.length(); i++) and access s[i] or s.at(i). Each iteration processes one character left to right. This mirrors array iteration.

String behaves like a character array with methods attached. Use index when you need position, compare neighbors, or modify specific locations. Index gives full control. Iterate backwards with for (int i = s.length()-1; i >= 0; i--).

Skip characters with i += 2. Start partway through. The index is yours to manipulate.