In this C++ tutorial, you shall learn how to iterate over the character of a given string using For loop, with example programs.
C++ Iterate over Characters of a String
To iterate over the characters of a string in C++, we can use foreach loop statement.
The following code snippet shows how to iterate over the characters of a string str using foreach loop.
</>
                        Copy
                        for (ch& : str) {
    //code
}
Example
In the following program, we take a string in str variable, and iterate over the characters of the string using for loop.
main.cpp
</>
                        Copy
                        #include <iostream>
using namespace std;
int main() {
    string str = "apple";
    for(char& ch : str) {
      cout << ch << endl;
   }
}
Output
a
p
p
l
e
Reference
In the above program(s), we have used the following C++ concepts. The links have been provided for reference.
- Initialize string using double quotes
 - C++ Foreach Loop
 
Conclusion
In this C++ Tutorial, we learned how to iterate over the characters of a string in C++ using Foreach loop statement.
