Basically i want to discover a situation: A user inputs a string lets say "sarah" and it is stored in string data type now if someone wants to do a operation on each character in the name sarah, how can it be done? like first take s from string then a then r and so on. Any built in function or better strategy? Thanks.
3 Answers
One simple method using :std::for_each
and a lambda function (C++11)
std::string str="sarah";
std::for_each( str.begin(), str.end(),
[](char &x){
//Play with x
}
);
Else, simply use a for loop
for(size_t i=0;i<str.size();++i)
{
//Play with str[i];
}
OR with C++11 :-
for(char& x : str) {
//Play with x;
}

- 46,614
- 9
- 72
- 119
you could use this:
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 = "sarah"; for(char& c : str) { do_things_with(c); }
Looping through the characters of a
std::string
with iterators:std::string str = "sarah"; 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: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 = "sarah"; for(char* it = str; *it; ++it) { do_things_with(*it); }
-
1Why you simply copied and pasted everything ? The link should be made as comment. – P0W Oct 24 '13 at 18:52
-
I've brought the necessary part from the answer and linked to it. I thought that's the way it works here. – Leon Oct 24 '13 at 18:56
Here's another one:
std::transform(begin(str), end(str), begin(str), do_things);
where do_things
is a character operation. I prefer transform
to for_each
because, to me, it better expresses that there is a transformation happening. However, if do_things
takes references to characters instead, then for_each
(or a loop) might be the better option.
Here's a more fleshed out example:
#include <iostream>
#include <string>
#include <algorithm> // for std::transform
#include <cctype> // for toupper/tolower
// Needed because toupper/tolower take and return ints, not chars.
char upper(char c) { return std::toupper(c); }
char lower(char c) { return std::tolower(c); }
int main() {
using namespace std;
string name;
cout << "Enter your name: ";
cin >> name;
// Make the name uppercase.
transform(name.begin(), name.end(), name.begin(), upper);
cout << "HI " << name << endl;
// Make the name lowercase.
transform(name.begin(), name.end(), name.begin(), lower);
cout << "hi " << name << endl;
}

- 7,746
- 1
- 26
- 39
-
like this? string input ; cout << "Enter the string into array " << endl ; cin >> input ; int size = strlen(input.c_str()); string array[size] = input then display array @POW – user2917063 Oct 24 '13 at 19:30
-
Just use `std::string`'s member function `size()`, instead of strlen. – bstamour Oct 24 '13 at 19:32
-
yeah, can you tell me how can i put code in comments properly here at stackoverflow – user2917063 Oct 27 '13 at 09:42
-
#include
#include
using namespace std;
int main(void)
{
string input ;
cout << "Enter the string into array " << endl ;
cin >> input ;
int size = strlen(input.c_str());
string array[size] = input ;
for(int i = 0; i < size ; i++)
{
cout << array[i] << endl;
}
system("PAUSE");
} <\code><\br> i think its not valid please clarify.
– user2917063 Oct 24 '13 at 19:21