-6

I'm supposed to print out a string input backwards. For example:

Enter the string to reverse: string
gnirts

This is what I have so far:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string input;

    cout<<"Enter the string to reverse: " << endl;
    cin>>input;

    int size = input.length();

    for(int i=size; i>0; i--)
    {
        cout<<input[i];
    }

    return 0;
}
Hulk
  • 6,399
  • 1
  • 30
  • 52

2 Answers2

3

Your initial array index points to \0, you need something like -

for(int i=size-1; i>=0; i--) // <-- like this

or

for(int i=size; i>0; i--)
{
  cout<<input[i-1]; // <-- like this
}

or you could use reverse

#include <algorithm> // <-- add this include
std::reverse(input.begin(), input.end()); // <-- reverse the input string.
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    It's not out of bounds, but he is starting with the \0 terminator of the string (nothing printable). – Petr Skocik Feb 06 '14 at 21:03
  • 1
    http://www.cplusplus.com/reference/string/string/operator%5B%5D/ " If pos is equal to the string length, the function returns a reference to a null character ('\0')." – Petr Skocik Feb 06 '14 at 21:07
  • 1
    @ThorX89 According to http://en.cppreference.com/w/cpp/string/basic_string/operator_at only if const overload is used. (Which I think it should, but could be important to note.) – Xarn Feb 06 '14 at 21:10
  • I was afraid my error would be something simple. I'll also try using reverse, looks simpler. Thanks for answering! – user3281370 Feb 06 '14 at 21:35
  • 1
    @ThorX89 +1 [you](http://stackoverflow.com/q/6077189/2970947) [tag:language-lawyer]! – Elliott Frisch Feb 06 '14 at 21:54
  • LOL. I didn't mean to quibble over terminology. It's just that out of bounds/within bounds in C/C++ in this case is the difference between a nonprintable character and the possibility of a nasty segfault. I didn't even know it was defined to return '\0', I just thought it might be (I guess I would've made it to mimic C the way it does) so I went ahead and looked it up. – Petr Skocik Feb 06 '14 at 22:19
2

The simplest way is to use standard algorithm std::reverse_copy

std::reverse_copy( input.begin(), input.end(), std::ostream_iterator<char>( std::cout ) ):
std::cout << std::endl;

It is the simplest way because you will not make a mistake in the control statement of the loop.:)

EDIT: I forgot to point out that you can use also algorithm std::copy.

std::copy( input.rbegin(), input.rend(), std::ostream_iterator<char>( std::cout ) );
std::cout << std::endl;

Also you can use a temporary object. For example

std::cout << std::string( input.rbegin(), input.rend() ) << std::endl;

if to use a loop then the correct loop will look

for ( std::string::size_type i = input.size(); i != 0; )
{
   std::cout << input[--i];
}
std::cout << std::endl;

or

for ( std::string::size_type i = input.size(); i != 0; --i )
{
   std::cout << input[i - 1];
}
std::cout << std::endl;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335