943,928 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1364
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 3rd, 2008
0

reversing a string

Expand Post »
I want help in reversing a string without using another array.
a string "Hello" should be printed as "olleh"
Please help.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunveer is offline Offline
10 posts
since Sep 2008
Oct 3rd, 2008
1

Re: reversing a string

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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 3rd, 2008
0

Re: reversing a string

i don't know algorithms and functions
Please provide the simplest code .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunveer is offline Offline
10 posts
since Sep 2008
Oct 3rd, 2008
1

Re: reversing a string

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 3rd, 2008
0

Re: reversing a string

>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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 3rd, 2008
0

Re: reversing a string

Just print it out in reverse?
Or reverse it "in place" in the string it's stored in, then print it out?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 3rd, 2008
1

Re: reversing a string

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
Reputation Points: 57
Solved Threads: 2
Junior Poster in Training
bhoot_jb is offline Offline
89 posts
since Mar 2008
Oct 6th, 2008
0

Re: reversing a string

Click to Expand / Collapse  Quote originally posted by sunveer ...
I want help in reversing a string without using another array.
a string "Hello" should be printed as "olleh"
Please help.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 8
Solved Threads: 1
Newbie Poster
STUDENT#101 is offline Offline
16 posts
since Aug 2008
Oct 6th, 2008
1

Re: reversing a string

>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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 9th, 2008
0

Re: reversing a string

Click to Expand / Collapse  Quote originally posted by Narue ...
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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 8
Solved Threads: 1
Newbie Poster
STUDENT#101 is offline Offline
16 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: From Java to C++
Next Thread in C++ Forum Timeline: C++ Compiler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC