CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2010
    Posts
    98

    [RESOLVED] string question

    hi all,

    suppose i've a string that has abc like:
    Code:
    string str;
    cin>>str;        // user enters abc
    for(int i=0; i<str.length(); i++)
    {
              //compare if str has 'a'
             // loop continues till find all character by character
    }
    is it possible with string class to find character by character?


    thanks..

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: string question

    Yes. Take a look at the interface of std::string.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: string question

    Quote Originally Posted by Aashi View Post
    hi all,

    suppose i've a string that has abc like:
    Code:
    string str;
    cin>>str;        // user enters abc
    for(int i=0; i<str.length(); i++)
    {
              //compare if str has 'a'
             // loop continues till find all character by character
    }
    is it possible with string class to find character by character?


    thanks..
    As an aside to the question and responses already given, if you intend "cin >> str;" to put the whole line into str, then you might want to consider using
    Code:
    std::getline(std::cin, str);
    since
    Code:
    cin >> str;
    will not put the whole line into str (it will only copy the first block of characters until it hits some white space) whereas std::getline will put the whole line into str.

  5. #5
    Join Date
    Oct 2010
    Posts
    98

    Re: string question

    thanks all,
    can anyone tell me which member function should i use for this purpose? from this site http://www.cplusplus.com/reference/string/string/
    i tried with find, compare..

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: string question

    Quote Originally Posted by Aashi View Post
    thanks all,
    can anyone tell me which member function should i use for this purpose?
    Your description of what you want to do is not clear.

    Let's start with this:
    Code:
    "abcabcabcda"
    So what do you want to do with this string?

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Oct 2010
    Posts
    98

    Re: string question

    Quote Originally Posted by Paul McKenzie View Post
    Your description of what you want to do is not clear.

    Let's start with this:
    Code:
    "abcabcabcda"
    So what do you want to do with this string?

    Regards,

    Paul McKenzie
    i want to make a logic for converting infix expression to postfix expression: for this i need to have stack and string. I can use char array for this, i just want to try with string. So, how can i compare a character with a string having abc/e+ ??

    thanks...

  8. #8
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: string question

    Quote Originally Posted by Aashi View Post
    thanks all,
    can anyone tell me which member function should i use for this purpose? from this site http://www.cplusplus.com/reference/string/string/
    i tried with find, compare..
    In the link: Look what they wrote after "Element access:"

    Regards
    PA

  9. #9
    Join Date
    Oct 2010
    Posts
    98

    Re: string question

    got it... thanks..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured