1

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.

user2917063
  • 33
  • 1
  • 6

3 Answers3

4

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;
   }
P0W
  • 46,614
  • 9
  • 72
  • 119
  • let me discover a bit this new thing to me for_each on web! – user2917063 Oct 24 '13 at 18:42
  • to use array style approach first i will have to store character by character in the array, for this user will have to press enter after each character like s->enter then a->enter and so on right? in this situation whole string will be enter all together like sarah->enter. – user2917063 Oct 24 '13 at 18:51
  • @user2917063 I've simply to say **what** ? well `std::cin>>str;` will read input from standard input full string, then you can iterate over characters . I suggest you a [good C++ book](http://stackoverflow.com/q/388242/1870232) – P0W Oct 24 '13 at 18:56
  • #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
  • @user2917063 You're doing it wrong See [here](http://ideone.com/kh1dqY) Also no need for `strlen` on `std::string`, just use `input.size()` – P0W Oct 24 '13 at 19:26
  • for_each returns ASCII code of char or string of ones and zeros, – user2917063 Oct 27 '13 at 09:46
1

you could use this:

  1. 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);
    }
    
  2. 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);
    }
    
  3. 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]);
    }
    
  4. Looping through the characters of a null-terminated character array:

    char* str = "sarah";
    for(char* it = str; *it; ++it) {
        do_things_with(*it);
    }
    

see: For every character in string

Community
  • 1
  • 1
Leon
  • 867
  • 7
  • 13
  • 1
    Why 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
1

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;
}
bstamour
  • 7,746
  • 1
  • 26
  • 39