As the title says, how can I seperate a string into individual characters in c++? For example, if the string is "cat" how can I separate that into the characters c,a, and t?
Thanks
As the title says, how can I seperate a string into individual characters in c++? For example, if the string is "cat" how can I separate that into the characters c,a, and t?
Thanks
By using the operator[]
. e.g.
std::string cat{"cat"};
if(cat[0] == 'c')
//stuff
If you're using std::string
you can simply use .c_str( )
that will give you an array of the chars.
In c++11
you could also do:
for( auto c : a )
{
cout << c << '\n';
}
If you want to store them in a vector:
string str("cat");
vector<char> chars(str.begin(), str.end());
for (char c : chars)
cout << c << endl;