Sign in to follow this  

Reversing a string?!

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

If you intended to correct an error in the post then please contact us.

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 this post


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


Edit: Added header file and namespace.

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

EDIT: Sorry completely misread what one of the guys had said don't worry about the question i just asked as its a load of rubbish

Cheers

Neil

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this