-4

I'm a beginner at coding and was given a project by a friend to reverse the order of a string inputted by the user, however when I run this code the program just repeatedly prints the string inputted many times over and I'm not sure whats wrong.

For instance, I input "hi", it just prints "hi" many times. I have tried using cin, getline and scanf (as recommended by a friend), but to no avail...

#include <iostream>

using namespace std;

int main()
{
    char arr[5];
    getline(cin, arr);
    for(int x=4; x>=0; x--){
        cout << arr << endl;
    }
    return 0;
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Clovis Nyu
  • 27
  • 2
  • 11
  • 2
    Printing a string in reverse order and actually reversing a string are two different things. Your title asks about actually reversing a string but you are attempting to print in reverse order. What are you actually trying to achieve? – taskinoor Aug 13 '16 at 07:26
  • If you remove the loop and just keep `cout << arr << endl;`, what does it print? And why would it print something else if you put it inside a loop? – molbdnilo Aug 13 '16 at 07:33
  • On an unrelated note, the fact that one of your first programs had a bug and your instinct was to examine everything *except* the logic of your reversal method is not a good sign. – molbdnilo Aug 13 '16 at 07:39
  • Possible duplicate of [How do you reverse a string in place in C or C++?](http://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c) – meJustAndrew Aug 13 '16 at 07:39
  • 2
    As this is C++, use [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) and [`std::reverse`](http://en.cppreference.com/w/cpp/algorithm/reverse) – Bart van Nierop Aug 13 '16 at 07:44
  • I recommend not writing hybrid C/C++ code. – Fang Aug 13 '16 at 07:58

5 Answers5

1

Since the question is tagged C++, you should use C++ constructs such as std::string and std::reverse. This will result in more readable and understandable code.

#include <string>
#include <iostream>
#include <algorithm>

int main()
{
    std::string input;
    std::getline(std::cin, input);
    std::reverse(input.begin(), input.end());
    std::cout << input << std::endl;
    return 0;
}
Community
  • 1
  • 1
Bart van Nierop
  • 4,130
  • 2
  • 28
  • 32
0

This line is wrong:

cout << arr << endl;

You have to use the index operator [] like this:

cout << arr[x];

Note that there is no endl, as that would print every character in a new line.

Using arr[x] gives you the element of the array (or character of the string if you will) at index x. Please note that element indexes in C++ start at 0. So the first element is arr[0], second arr[1], and so on.

Also, why use a C-style char array of only size 5? You can use the C++ std::string just as effectively and it will work for larger strings:

string x;
getline(cin, x);

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

cout << endl;

Hope this helps.

0

When you write, cout << arr << endl; you are printing the entire string in each iteration of the loop. Instead, you wish to print the character at the index x, so you should write it as cout << arr[x]; If you use endl inside the loop, you will get a new line after each character.

Moreover, in C++, there is an easier way to deal with strings, using the string library. Then, you need not specify the number of characters in your string beforehand, and helps if the user needs to enter more than 4 characters.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string arr;
    getline(cin, arr);
    for(int x=arr.size()-1; x>=0; x--){
        cout << arr[x];
    }
    cout << endl;
    return 0;
}
GoodDeeds
  • 7,956
  • 5
  • 34
  • 61
  • So by including the string library, the code reads strings by their individual characters? And in turn this allows me to treat the string as if it were an array? – Clovis Nyu Aug 13 '16 at 14:51
  • @ClovisNyu You can reference any index of the string just like an array, and not worry about how long an input the user might give. – GoodDeeds Aug 13 '16 at 14:52
0

What's happening is you're sending the entire contents of arr to cout 5 times. What you want instead is to print each character in reverse; to do this, you need to send only one character of arr at a time inside your for loop:

cout << arr[x] // This sends the character at index x to cout
cout << arr    // This sends the entire array to cout

Also, you should have cout << endl after the for loop; otherwise, you'll print a newline character after each letter.

0

Alternate solution using iterators:

#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>

int main()
{
   std::string input;
   getline(std::cin, input);

   for (std::string::reverse_iterator rit=input.rbegin(); rit!=input.rend(); ++rit)
   std::cout << *rit;

   return 0;
}
Fang
  • 2,199
  • 4
  • 23
  • 44