| | |
reversing a string
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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)
#include <algorithm> #include <string> std::string s = "hello"; std::reverse ( s.begin(), s.end() );
Last edited by Narue; Oct 3rd, 2008 at 10:01 am.
I'm here to prove you wrong.
>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.
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.
•
•
Join Date: Aug 2008
Posts: 16
Reputation:
Solved Threads: 1
•
•
•
•
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)
#include<iostream> #include<string> using namespace std; int main() { string msg; cout<<enter massage"<<endl; cin.ignore(); getline(cin,msg); int z=msg.length(); fo(int n=z;n>-1;n--) { cout<<msg[n]; } cout<<endl; return 0; }
Last edited by Narue; Oct 6th, 2008 at 9:18 am. Reason: added code tags
>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
>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
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:
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 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)
string::size_type n = msg.length(); while ( --n != (string::size_type)-1 ) cout<< msg[n];
I'm here to prove you wrong.
•
•
Join Date: Aug 2008
Posts: 16
Reputation:
Solved Threads: 1
•
•
•
•
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)
#include <algorithm> #include <string> std::string s = "hello"; std::reverse ( s.begin(), s.end() );
![]() |
Similar Threads
- Reversing a String which is inputed. (Java)
- Reversing a string using recursion (Java)
- Reversing a string (C++)
- I Need Assembly help w/reversing strings (Assembly)
- Reversing a string? (C++)
Other Threads in the C++ Forum
- Previous Thread: From Java to C++
- Next Thread: C++ Compiler
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






