accessing elements of a string in c++ code example
Example 1: c++ string element access
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string");
for (int i=0; i<str.length(); ++i)
{
std::cout << str[i];
}
return 0;
}
Example 2: how to get a section of a string in c++
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str="We think in generalities, but we live in details.";
string str2, str3;
size_t pos;
str2 = str.substr (12,12);
pos = str.find("live");
str3 = str.substr (pos);
cout << str2 << ' ' << str3 << endl;
return 0;
}