Public Group

# Reversing a string?!

This topic is 4309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, I have recently been mucking about with how to try and reverse a string entered by a user. Anyway, as you may have guessed from the fact i am posting here i am not having much luck. This is what i have so far
[source lang = "cpp"]

#include <iostream>
#include <string>

using namespace std;

int length;
int tempLength;
string sentence;
string revString;

int main()
{
cout<<"Enter a sentence"<<endl;
getline(cin,sentence);
length = sentence.length();
cout<<length<<endl;
tempLength = length;
cout<<tempLength<<endl;;
cout<<sentence[length-4];
for(int i=0;i<tempLength;i++)
{
cout<<sentence[length]<<endl;
length--;
}
for (int j=0;j<tempLength;j++)
{
cout<<revString[j]<<endl;
}
//	cout<<length<<endl;
cout<<endl;
cout<<sentence<<endl;

system("PAUSE");
return 0;
}


Now everything works up until the 2nd for loop where i try and populate the reversed string. I have just got it working using a char array which i guess i could then easily turn back into a straing but is there any way that i could do this without having to use the char array as an intermediate storage? Cheers Neil

##### Share on other sites
#include <algorithm>std::reverse(sentence.begin(), sentence.end());

[Edited by - tstrimp on September 27, 2006 9:55:13 AM]

##### Share on other sites
Well your program doesn't do anything with revString apart from try to output some characters of it, as you've used the default constructor for revString which gives you an empty string this is going to result in either a load of garbage output or a crash.

tstrimp's answer is the short and sweet version of how you could do this. Note that it's using the standard library function std::reverse which is in the header algorithm IIRC.

##### Share on other sites
In case all you need to do is output some reversed input, you can copy your string's contents backwards into the output stream.

For example:
void Dummy(const std::string & rString, const std::string & rDelimiter = "", std::ostream & rOutputStream = std::cout){	std::copy(rString.rbegin(), rString.rend(), std::ostream_iterator <char> (rOutputStream, rDelimiter.c_str()));	rOutputStream.flush();}//...Dummy("Hello"); //"olleH"Dummy("Hello", "\n"); //"o\nl\nl\ne\nH\n"

##### Share on other sites
Tstrimp has the right solution for an in-place reversal.

If you want to construct a reversed copy, do it thusly:
std::string forward = "abcdefghijk";std::string backward(forward.rbegin(), forward.rend());

rbegin() and rend() produce reverse iterators, which go from the end of the string towards the beginning. And as you can see with the definition of backward, std::string also has a constructor that takes a pair of iterators to define the sequence from which it gets its characters ... in this case, all of forward, from back to front.

##### Share on other sites
Sorry it took so long for a reply.

Thanks for all the responses aswell, as always most helpful.

is revString a library function or something? And is the fact that i have used that name without passing any parameters etc a reason for the error?

Cheers

Neil

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002297
×