...

View Full Version : How do I reverse a string in C++?



Eckenrode
10-04-2007, 08:24 PM
I'm trying to create a C++ program for a friend that will take a line of text as an input and output the same text, but reversed. It should be simple enough, and I know I could have done it back when I was actually enrolled in a C++ course, but I seem to be missing something. Any advice you could give me would be helpful.

This is my function for doing the actual reversing:


string Reverse (string normal)
{
int len = normal.length ();
int n;
string reverse = normal;
for (n = 0;n < len; n++)
{
reverse[len - n] = normal[n];
}
cout << reverse << endl;
return reverse;
}


This works for everything except for the first letter in the string, which is replaced with a space (so if "coffee" was input, " eeffo" would be output). I can think of a few roundabout fixes, but they're all about as elegant as using a staple gun on a two page document.

ralph l mayo
10-04-2007, 08:58 PM
#include <algorithm>

//...

std::string reverse(const std::string& normal)
{
std::string rev(normal);
std::reverse(rev.begin(), rev.end());
return rev;
}

Spookster
10-04-2007, 09:23 PM
What are the specifics of your friends homework assignment?

Eckenrode
10-04-2007, 09:40 PM
What are the specifics of your friends homework assignment?

It's not homework. I have a friend who wants to dress up as Zatanna (http://en.wikipedia.org/wiki/Zatanna_Zatara) for a Halloween party, and I told her that she should actually talk backwards during the party. She said she would only do it if I wrote a program that would make it easier. We'll probably put it on her laptop so she can use it easily.

Also, thanks for the quick replies.

Spookster
10-04-2007, 11:12 PM
In that case unless you need to do it in C++ and since your friend will probably want to have some kind of easy to use GUI you may want to consider doing it in HTML using Javascript to perform the logic and an HTML form to type her input into and save it locally on her laptop.

Dunna
10-05-2007, 06:07 AM
With an array of chars it would simply be:


strrev(reverse);

I'm not sure if that will work with the string class though.

Eckenrode
10-05-2007, 05:49 PM
In that case unless you need to do it in C++ and since your friend will probably want to have some kind of easy to use GUI you may want to consider doing it in HTML using Javascript to perform the logic and an HTML form to type her input into and save it locally on her laptop.

That sounds like it would be best, but I don't know any Java. The only thing I've learned to program in is C++, and as you can seen, I'm a little rusty at that, too.

Spookster
10-05-2007, 06:43 PM
That sounds like it would be best, but I don't know any Java. The only thing I've learned to program in is C++, and as you can seen, I'm a little rusty at that, too.

Java and Javascript are not the same thing. Java is a high level programming language similar to C++. Javascript is a scripting language designed for web browsers. You can get help with that in our Javascript forum. You could probably also google a javascript text reversing script. Like this one

http://javascript.internet.com/forms/text-reverser.html

avikb
09-19-2009, 01:27 AM
string reverse(string src) { return string(src.rbegin(), src.rend()); }



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum