reversing a string

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 10
Reputation: sunveer is an unknown quantity at this point 
Solved Threads: 0
sunveer sunveer is offline Offline
Newbie Poster

reversing a string

 
0
  #1
Oct 3rd, 2008
I want help in reversing a string without using another array.
a string "Hello" should be printed as "olleh"
Please help.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,719
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 735
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: reversing a string

 
1
  #2
Oct 3rd, 2008
Since you didn't give nearly enough information, or provide even a rudimentary attempt, this is all you get. Here's how a real-world programmer would do it:
  1. #include <algorithm>
  2. #include <string>
  3.  
  4. std::string s = "hello";
  5. std::reverse ( s.begin(), s.end() );
Last edited by Narue; Oct 3rd, 2008 at 10:01 am.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 10
Reputation: sunveer is an unknown quantity at this point 
Solved Threads: 0
sunveer sunveer is offline Offline
Newbie Poster

Re: reversing a string

 
0
  #3
Oct 3rd, 2008
i don't know algorithms and functions
Please provide the simplest code .
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,719
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 735
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: reversing a string

 
1
  #4
Oct 3rd, 2008
>i don't know algorithms and functions
Yes, I figured as much.

>Please provide the simplest code .
It doesn't work that way. You prove that you've made an honest attempt, then we provide help to boost you along.
Last edited by Narue; Oct 3rd, 2008 at 10:12 am.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: reversing a string

 
0
  #5
Oct 3rd, 2008
>Please Provide The Simplest Code.
Well wont it be simple If You create a function to interchange the first and last char and then the last but one and second character and so on....
This should be done until They Come to the middle of the word.

Try Implementing a FLOWCHART.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: reversing a string

 
0
  #6
Oct 3rd, 2008
Just print it out in reverse?
Or reverse it "in place" in the string it's stored in, then print it out?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 89
Reputation: bhoot_jb is on a distinguished road 
Solved Threads: 2
bhoot_jb bhoot_jb is offline Offline
Junior Poster in Training

Re: reversing a string

 
1
  #7
Oct 3rd, 2008
i think first of all you really need to attempt it by yourself. People here will help you only if you have made some honest attempt
Bhoot
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 16
Reputation: STUDENT#101 is an unknown quantity at this point 
Solved Threads: 1
STUDENT#101 STUDENT#101 is offline Offline
Newbie Poster

Re: reversing a string

 
0
  #8
Oct 6th, 2008
Originally Posted by sunveer View Post
I want help in reversing a string without using another array.
a string "Hello" should be printed as "olleh"
Please help.
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6. string msg;
  7. cout<<enter massage"<<endl;
  8. cin.ignore();
  9. getline(cin,msg);
  10. int z=msg.length();
  11. fo(int n=z;n>-1;n--)
  12. {
  13. cout<<msg[n];
  14. }
  15. cout<<endl;
  16. return 0;
  17. }
  18.  
this is all ive got
Last edited by Narue; Oct 6th, 2008 at 9:18 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,719
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 735
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: reversing a string

 
1
  #9
Oct 6th, 2008
>this is all ive got
I suppose it could be worse.

>cout<<enter massage"<<endl;
You forgot the opening double quote for the string. This will cause your code to fail compilation.

>cin.ignore();
There's exactly no point to this line. In fact, it'll probably surprise users.

>int z=msg.length();
Type mismatch. length returns an unsigned quantity, the type of which can be acquired by saying string::size_type instead of int.

>fo(int n=z;n>-1;n--)
"fo", huh? I don't recall that on the list of C++ keywords. You also have an off-by-one error where you access msg[msg.length()] . Indexing in C++ is zero-based, so you're accessing the string beyond the last character.

In the interests of helping you improve, I'll teach you a trick for using unsigned types in a loop that counts down to zero. It's helpful in cases like this where you need to stop after zero, but can't actually get below zero. The trick is to rely on unsigned wraparound:
  1. string::size_type n = msg.length();
  2.  
  3. while ( --n != (string::size_type)-1 )
  4. cout<< msg[n];
When you cast -1 to an unsigned type, you get the largest value of that unsigned type. This has the same effect as decrementing an unsigned type with a value of zero.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 16
Reputation: STUDENT#101 is an unknown quantity at this point 
Solved Threads: 1
STUDENT#101 STUDENT#101 is offline Offline
Newbie Poster

Re: reversing a string

 
0
  #10
Oct 9th, 2008
Originally Posted by Narue View Post
Since you didn't give nearly enough information, or provide even a rudimentary attempt, this is all you get. Here's how a real-world programmer would do it:
  1. #include <algorithm>
  2. #include <string>
  3.  
  4. std::string s = "hello";
  5. std::reverse ( s.begin(), s.end() );
yes i am sorry about the qoute but as for cin.ignore() I have to disagree, I'm using linux and it is neccesary for me to include it. In a way I surpose I was wrong for forgettng the semicolon but before telling me that it the ci.ignore is not neccesary perhaps you should have asked considered the fact that we all use different compilers ohh and by the way I'm still studying programming so I go for simple sollutions that I understand sorry if I was wrong for jst trying to help thanx.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC