Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I reverse a string in C++?

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

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Code:
    #include <algorithm>
    
    //...
    
    std::string reverse(const std::string& normal)
    {
    	std::string rev(normal);
    	std::reverse(rev.begin(), rev.end());
    	return rev;
    }

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    What are the specifics of your friends homework assignment?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #4
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Spookster View Post
    What are the specifics of your friends homework assignment?
    It's not homework. I have a friend who wants to dress up as Zatanna 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.

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    Regular Coder
    Join Date
    May 2004
    Location
    New Hampshire, America
    Posts
    246
    Thanks
    0
    Thanked 2 Times in 2 Posts
    With an array of chars it would simply be:
    Code:
    strrev(reverse);
    I'm not sure if that will work with the string class though.

  • #7
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Spookster View Post
    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.

  • #8
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Eckenrode View Post
    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
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #9
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    string reverse(string src) { return string(src.rbegin(), src.rend()); }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •