I want help in reversing a string without using another array.
a string "Hello" should be printed as "olleh"
Please help.

Recommended Answers

All 10 Replies

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:

#include <algorithm>
#include <string>

std::string s = "hello";
std::reverse ( s.begin(), s.end() );

i don't know algorithms and functions
Please provide the simplest code .

>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.

>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.

Just print it out in reverse?
Or reverse it "in place" in the string it's stored in, then print it out?

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 :)

commented: Well said +22

I want help in reversing a string without using another array.
a string "Hello" should be printed as "olleh"
Please help.

#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;
}

this is all ive got

>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:

string::size_type n = msg.length();

while ( --n != (string::size_type)-1 )
  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.

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:

#include <algorithm>
#include <string>

std::string s = "hello";
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.

>as for cin.ignore() I have to disagree, I'm using linux and it is neccesary for me to include it.
I reject your disagreement. Most likely you think it's necessary because you've misunderstood the solution to another problem unrelated to what you're actually doing.

Here's how it works. At program startup the standard input stream is pristine; there are no characters present, so calling cin.ignore will block and ignore a single character. Then getline will be called and read any subsequent characters, also blocking if necessary:

#include <iostream>
#include <string>

int main()
{
  std::string line;

  std::cin.ignore();
  getline ( std::cin, line );

  std::cout<<'>'<< line <<"<\n";
}

If you type "testing", the output will be ">esting<". The first character will always be extracted and thrown away, period. Further, because the stream is empty when the program starts, there's no requirement that you ignore the first character unless your program's logic depends on it for some reason (which it doesn't in this case).

>perhaps you should have asked considered the fact that we all use different compilers
What I've described is standard C++ behavior. Yes, we all use different compilers (I use several all by myself), but unless yours is obscure and very broken, it will conform to the standard behavior.

commented: :) +17
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.