Iterating Over Strings

Can I Iterate Over a String?

Yes, you can loop over a string one character at a time in C++. See range-based for loops, index loops, iterators and C-style strings, with examples.

On this page
  1. Yes, you can loop over every character
  2. The range-based for loop
  3. An index loop
  4. Iterators
  5. C-style strings
  6. Stay inside the string
  7. Change characters while looping
  8. Where to go next

Yes, you can loop over every character

Yes. In C++ you can iterate over a string one character at a time, from the first character to the last. The usual choice is a range-based for loop, which reads each character without any index math. Index loops, iterators and pointer loops also work, and each one suits a different situation.

The range-based for loop

A range-based for loop needs C++11 or later. This example prints each character of a short string:

#include <iostream>
#include <string>

int main() {
    std::string word = "Hi!";

    for (char c : word) {
        std::cout << c << '\n';
    }

    return 0;
}

This prints H, i and !, one per line.

An index loop

When you need the position of each character, count with an index from 0 up to, but not including, size():

#include <iostream>
#include <string>

int main() {
    std::string word = "Hi!";

    for (std::string::size_type i = 0; i < word.size(); ++i) {
        std::cout << i << ' ' << word[i] << '\n';
    }

    return 0;
}

This prints 0 H, 1 i and 2 !.

Iterators

Iterators work like pointers that move through the string. begin() points at the first character, end() points just past the last, and ++ moves to the next character:

#include <iostream>
#include <string>

int main() {
    std::string word = "Hi!";

    for (auto it = word.begin(); it != word.end(); ++it) {
        std::cout << *it << '\n';
    }

    return 0;
}

C-style strings

A C-style string is a char array that ends with the null character '\0'. A pointer loop stops when it reaches that terminator:

#include <iostream>

int main() {
    const char* text = "Hi!";

    for (const char* p = text; *p != '\0'; ++p) {
        std::cout << *p << '\n';
    }

    return 0;
}

Stay inside the string

The loop has to stop at the end of the text. For a std::string, use size() or end() as the limit, not a fixed number. If the string is empty, the loop body runs zero times.

Reading past the end is undefined behavior, and the program may crash or read garbage. When an index might be wrong, at() checks it and throws std::out_of_range instead.

Change characters while looping

A range-based loop with a plain char variable gives you a copy of each character. To change the characters in place, use char& as the loop variable type instead.

Where to go next

For a full guide, including reverse loops, passing a string to a function and common mistakes, see how to iterate through a string in c. If you want to walk the text backward, how to reverse a string in c shows the common approaches.

The complete guide to iterating over strings

How to Iterate Through a String in C++
Read the guide

Keep exploring

Guides from other topics

All topics →