How would I do a for loop on every character in string in C++?
10 Answers
Looping through the characters of a
std::string
, using a range-based for loop (it's from C++11, already supported in recent releases of GCC, clang, and the VC11 beta):std::string str = ???; for(char& c : str) { do_things_with(c); }
Looping through the characters of a
std::string
with iterators:std::string str = ???; for(std::string::iterator it = str.begin(); it != str.end(); ++it) { do_things_with(*it); }
Looping through the characters of a
std::string
with an old-fashioned for-loop:std::string str = ???; for(std::string::size_type i = 0; i < str.size(); ++i) { do_things_with(str[i]); }
Looping through the characters of a null-terminated character array:
char* str = ???; for(char* it = str; *it; ++it) { do_things_with(*it); }
A for loop can be implemented like this:
string str("HELLO");
for (int i = 0; i < str.size(); i++){
cout << str[i];
}
This will print the string character by character. str[i]
returns character at index i
.
If it is a character array:
char str[6] = "hello";
for (int i = 0; str[i] != '\0'; i++){
cout << str[i];
}
Basically above two are two type of strings supported by c++. The second is called c string and the first is called std string or(c++ string).I would suggest use c++ string,much Easy to handle.
In modern C++:
std::string s("Hello world");
for (char & c : s)
{
std::cout << "One character: " << c << "\n";
c = '*';
}
In C++98/03:
for (std::string::iterator it = s.begin(), end = s.end(); it != end; ++it)
{
std::cout << "One character: " << *it << "\n";
*it = '*';
}
For read-only iteration, you can use std::string::const_iterator
in C++98, and for (char const & c : s)
or just for (char c : s)
in C++11.
I don't see any examples using a range based for loop with a "c string".
char cs[] = "This is a c string\u0031 \x32 3";
// range based for loop does not print '\n'
for (char& c : cs) {
printf("%c", c);
}
not related but int array example
int ia[] = {1,2,3,4,5,6};
for (int& i : ia) {
printf("%d", i);
}
For C-string (char []
) you should do something like this:
char mystring[] = "My String";
int size = strlen(mystring);
int i;
for(i = 0; i < size; i++) {
char c = mystring[i];
}
For std::string
you can use str.size()
to get its size and iterate like the example , or could use an iterator:
std::string mystring = "My String";
std::string::iterator it;
for(it = mystring.begin(); it != mystring.end(); it++) {
char c = *it;
}
for (int x = 0; x < yourString.size();x++){
if (yourString[x] == 'a'){
//Do Something
}
if (yourString[x] == 'b'){
//Do Something
}
if (yourString[x] == 'c'){
//Do Something
}
//...........
}
A String is basically an array of characters, therefore you can specify the index to get the character. If you don't know the index, then you can loop through it like the above code, but when you're making a comparison, make sure you use single quotes (which specifies a character).
Other than that, the above code is self explanatory.
you can get every char in a string by using the at function of string library, like i did it like this
string words;
for (unsigned int i = 0; i < words.length(); i++)
{
if (words.at(i) == ' ')
{
spacecounter++; // to count all the spaces in a string
if (words.at(i + 1) == ' ')
{
i += 1;
}
this is just a segment of my code but the point is you can access characters by stringname.at(index)
std::string
? – Mysticialchar
, Unicode code point, extended grapheme cluster? – Philippindex
part in the answers. – jww