How to Iterate Through String in C++

C-Plus-Plus-String

In this article, we are going to learn about How to Iterate Through String in C++. We can iterate on a string to get each character in a string. We will learn many ways to iterate over a string in C++. We will use the below techniques to iterate on a string in C++.

  • Using for Loop index to Iterate Through String in C++
  • Using for Loop and at() function to Iterate Through String in C++.
  • Using Range Based Loop to Iterate Through String in C++.

1.Using for Loop index to Iterate Through String in C++


The first method to iterate over a string is by using the for loop and the square bracket indices. By using this method we can access each and every character in a string. We can run our loop to the length of the string to make sure we do not exceed the string length. Let us understand this with the help of an example.

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str = "C++ String Tutorials!";

    for (int i = 0; i < str.length(); ++i) 
    {
        cout << "Character at Index" << i << " is : " << str[i] << endl;
    }

    return 0;
}

Output

Character at Index0 is : C
Character at Index1 is : +
Character at Index2 is : +
Character at Index3 is :  
Character at Index4 is : S
Character at Index5 is : t
Character at Index6 is : r
Character at Index7 is : i
Character at Index8 is : n
Character at Index9 is : g
Character at Index10 is :  
Character at Index11 is : T
Character at Index12 is : u
Character at Index13 is : t
Character at Index14 is : o
Character at Index15 is : r
Character at Index16 is : i
Character at Index17 is : a
Character at Index18 is : l

2. Using for Loop and at() function to Iterate Through String in C++


Our next method to iterate over a string is similar to the first one which we just learned. In this example, we are using the at() function to access the character at each index in a string.at() function takes the index as an argument and it gives us the character present at that location in a string. In this method also, we are running the loop to the length of the string and making sure we are not exceeding the length of the given string. let us jump to our example to understand this with the help of code.

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str = "C++ String";

    for (int i = 0; i < str.length(); ++i) 
    {
        cout << "Character at Index" << i << " is : " << str.at(i) << endl;
    }

    return 0;
}

Output

Character at Index0 is : c
Character at Index1 is : +
Character at Index2 is : +
Character at Index3 is :  
Character at Index4 is : S
Character at Index5 is : t
Character at Index6 is : r
Character at Index7 is : i
Character at Index8 is : n
Character at Index9 is : g


3. Using Range Based Loop to Iterate Through String in C++


The third and last method that we are going to learn today is by using the Range-based loop method. In this example, we are going to make use of the Range-based loop instead of for loop which we used in our above examples. Range-based loops do not use the iterator variables, instead, they use their own indexing to access each and every element of the iterable loop. But just to make the output the same as we did in the above two examples we will be using a dummy index to format output.

#include <iostream>
#include <string>

using namespace std;

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

    size_t i = 0;
    for (char chr : str) 
    {
        cout << "Char at Index" << i++ << " is : " << chr << endl;
    }

    return 0;
}

Output

Char at Index0 is : D
Char at Index1 is : e
Char at Index2 is : v
Char at Index3 is : e
Char at Index4 is : n
Char at Index5 is : u
Char at Index6 is : m


Summary

In this post we have learned how to Iterate Through String in C++