Iterate over every character of a string in C++

In this article, we will discuss how to loop through every character of a string in C++.

Table Of Contents

Method 1: Using a for loop

We iterate over all characters of a string, by simply using a range based for loop. Where we will get access to each individual character of string. Let’s see the example,

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcde";

    // iterating every character by for loop
    for(auto ch : str)
    {
        cout << ch << ", ";
    }
    return 0;
}

Output

a, b, c, d, e,

It iterated over all characters of string, and printed each of them on the console.

Method 2: Using Iterator and While Loop

Using the begin() function of string, get access to iterator pointing to the first character of string. Then inside a while loop, keep on incrementing the iterator, till it reaches the end of string. During the loop, we can access each character of string independently. Let’s see an example,

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcde";

    // Get iterator pointing to first character
    auto it = str.begin();

    // iterating every character by for loop
    while( it != str.end())
    {
        cout << *it << ", ";
        it++;
    }

    return 0;
}

Output

a, b, c, d, e,

It iterated over all characters of string, and printed each of them on the console.

Method 3: Using STL ALgorithm for_each()

We can apply a lambda function on each character of string, using for_each() function. Inside the lambda function, we are printing each character individually. Let’s see an example,

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    string str = "abcde";

    // Iterate over every character of string
    std::for_each(
            str.begin(),
            str.end(),
            [](const char& ch)
            {
                cout<< ch << ", ";
            });

    return 0;
}

Output

a, b, c, d, e,

It iterated over all characters of string, and printed each of them on the console.

Method 4: By Overloading operator<< for string

We can overload the << operator for string object. Inside this overloaded implementation, we can iterate over each character of string, and add that to the given output stream. After defining this function, we can directly pass the string object to the console for printing. Let’s see an example,

#include <iostream>
#include <string>

using namespace std;

std::ostream &operator<< (
                std::ostream& osObj,
                const std::string &str)
{
    for (char const &ch : str)
    {
        osObj << ch << ", ";
    }
    return osObj;
}

int main()
{
    string str = "abcde";

    cout<< str << std::endl;

    return 0;
}

Output

a, b, c, d, e,

It iterated over all characters of string, and printed each of them on the console.

Method 5: Using index based for loop

This is the most basic solution. In this, we are going to iterate over a range of numbers i.e. from o till N, where N is the size of string. During iteration, for each index number, we will access the character at that index position from string, and print it on console. Let’s see the example,

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcde";

    for(int i = 0; i < str.size(); i++)
    {
        cout<< str[i] << ", ";
    }
    return 0;
}

Output

a, b, c, d, e,

It iterated over all characters of string, and printed each of them on the console.

Summary

We used different methods to iterate over every character in a string in C++. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top