How to print each character of a string in C++

In this article, we will discuss how to print each character of a string in C++.

Table Of Contents

Method 1: Using a range based for loop

We loop through all characters of a string, by using a range based for loop. Inside the loop, we will print each individual character of string. Let’s see the example,

#include <iostream>
#include <string>

using namespace std;

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

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

Output

T
e
s
t
i
n
g

It printed all the characters of string on separate lines.

Advertisements

Method 2: Using Iterator and While Loop

Steps are as follows,

  • Use the string::begin() function to access the iterator pointing to the first character of string.
  • Use the string::end() function to access the iterator pointing to the end of string.
  • Use a while loop iterate from first iterator till the end iterator, and during the loop, print each character of string independently.

Let’s see an example,

#include <iostream>
#include <string>

using namespace std;

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

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

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

    return 0;
}

Output

T
e
s
t
i
n
g

It printed all the characters of string on separate lines.

Method 3: Using STL ALgorithm for_each()

Apply a lambda function on each character of string, using for_each() function. Inside the lambda function, we can print that character of the string. Let’s see an example,

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

using namespace std;

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

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

    return 0;
}

Output

T
e
s
t
i
n
g

It printed all the characters of string on separate lines.

Method 4: By Overloading operator<< for string

We can overload the << operator for string to print each character of string in a newline. 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 << endl;
    }
    return osObj;
}

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

    cout<< str << std::endl;

    return 0;
}

Output

T
e
s
t
i
n
g

It printed all the characters of string on separate lines.

Method 5: Using index based for loop

This is the simplest solution. Loop over all characters in string using a for loop, and print each character one by one. Let’s see an example,

#include <iostream>
#include <string>

using namespace std;

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

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

Output

T
e
s
t
i
n
g

It printed all the characters of string on separate lines.

Summary

We used different techniques to print each character of a string in C++. Thanks.

Advertisements

Thanks for reading.

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