CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2002
    Posts
    96

    string manipulation

    Hi All,

    I need the sample code for following questions.


    I have one string : "my name is sam"
    Now I have to reverse it : "mas si eman ym"

    also count the no of words in the string
    also revert like this : "sam is name my"

    Could anyone please help me by providing sample code ? I don't want to use string class.

    Looking forward for your valuable input.

    Thanking you in advance.

    Regds,
    Sam

  2. #2
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762

    Re: string manipulation

    I suspect your two posts so far have been homework questions.

    I'll give you some directions but not direct answers.

    To reverse a string, you could always use the begin() and end() functions in algorithm and sort().

    As for changing the word order, throw everything into a vector with getline using ' ' as a delimiter, then just put them back in the string however you want. You could count the number of words this way too.

  3. #3
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: string manipulation

    Quote Originally Posted by kasracer
    To reverse a string, you could always use the begin() and end() functions in algorithm and sort().
    Are you sure you meant "sort"? If he's trying to reverse the string, wouldn't the "reverse" algorithm be a better choice? I don't quite see how sort would help...
    Insert entertaining phrase here

  4. #4
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: string manipulation

    Samirdan, if you're indeed doing homework, then it will be obvious you got help if you use the suggestions mentioned above. I would try to work out a solution on your own -- which isn't a bad thing. It really isn't that bad. You need to learn how to do this if you're learning to program.
    Kevin Hall

  5. #5
    Join Date
    Apr 2002
    Posts
    61

    Re: string manipulation

    Assuming your class is using char * (you didn't want to use the string class) than each letter in the string can be pointed to rather easily
    assuming: char myStr[7] = "my name";

    then you can point to the first letter using myStr[0], and the nth letter in the string with myStr[n-1]. This should help you rearrange your letters but it is your homework.

  6. #6
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762

    Re: string manipulation

    Quote Originally Posted by wien
    Are you sure you meant "sort"? If he's trying to reverse the string, wouldn't the "reverse" algorithm be a better choice? I don't quite see how sort would help...
    Yeah you're right. My head was still into the vector problem I was having

  7. #7
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: string manipulation

    Quote Originally Posted by kasracer
    Yeah you're right. My head was still into the vector problem I was having
    That's what i thought!
    Quote Originally Posted by DevLip
    Assuming your class is using char *<snip>
    That approach would work fine for std::string (and most other string classes I have seen) as well, since it has it's operator [] overloaded.
    Insert entertaining phrase here

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