string iteration in c++ code example
Example 1: how to iterate in string in c++
#include<iostream>
using namespace std;
main() {
string my_str = "Hello World";
for(int i = 0; i<my_str.length(); i++) {
cout << my_str.at(i) << endl;
}
}
Example 2: how to iterate throguh a string in c++
void print(const std::string &s)
{
for (std::string::size_type i = 0; i < s.size(); i++) {
std::cout << s[i] << ' ';
}
}
Example 3: string iterator in c++
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string");
for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
std::cout << *it << endl;
std::cout << '\n';
return 0;
}