Skip to main content
GameDev.net gamedev.net
🔒 Locked

Reversing a string?!

Started by neilski_2003 Sep 27, 2006 at 8:52 AM 4 replies 1k views
Original Post
neilski_2003
neilski_2003
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
tstrimp
tstrimp
#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]
Monder
Monder
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.
raz0r
raz0r
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"
Fruny
Fruny
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
neilski_2003
neilski_2003
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.