-
January 20th, 2005, 10:59 PM
#1
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
-
January 20th, 2005, 11:06 PM
#2
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.
-
January 21st, 2005, 02:58 AM
#3
Re: string manipulation
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
-
January 21st, 2005, 08:50 AM
#4
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
-
January 21st, 2005, 12:36 PM
#5
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.
-
January 21st, 2005, 01:19 PM
#6
Re: string manipulation
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
-
January 22nd, 2005, 09:38 AM
#7
Re: string manipulation
Originally Posted by kasracer
Yeah you're right. My head was still into the vector problem I was having
That's what i thought!
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|