1

I'm trying to take a users input and reverse it for comparison later. Looks like I'm going in the right direction, but I can't seem to figure out why my method only returns one letter. It should return the entire Cstring reversed when I cout reversedInput.

#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
void reverseString(char arr[100]);
char reversedInput[100];


int main() {

    char input[100];


    cout<<"Please enter a string"<<endl;
    cin.getline(input, 100);
    cout<<"You entered "<<input<<endl;

    reverseString(input);
    cout<<"The reverse is "<<reversedInput<<endl;

    system("pause");
    return 0;
}

void reverseString (char arr[]) {
    int i;
    int j = strlen(arr) - 1;
    char temp;

    for (i = 0; i < j; i++, j--) {
        temp = arr[j];
        //        arr[j];
        reversedInput[i] = temp;
    }

}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Red Rabbit
  • 115
  • 1
  • 11
  • 3
    You never added the null terminator for reversedInput. – drescherjm Oct 30 '18 at 16:08
  • @drescherjm the OP doesn't have to. The target buffer is global, and used only once. It is zero filled, so the terminator (while expensive) is free. Not a great design, but it will work none-the-less. – WhozCraig Oct 30 '18 at 16:17
  • 1
    Why not just use `std::string`? It has `crbegin()` and `crend()` that give you exactly what you need. – Toby Speight Oct 30 '18 at 16:35

3 Answers3

2

The root problem is i < j in your conditional. As you descend the source string, j decrements while i increments, eventually meeting halfway up the string, and leaving you with half not being copied.

void reverseString(const char arr[]) 
{
    size_t len = strlen(arr);
    for (size_t i = 0; len-- > 0; ++i)
        reversedInput[i] = arr[len];
}

Note, the avoidance of computing strlen(arr)-1 is intentional. Consider what that becomes when/if you pass an empty string to this function. (Hint: unsigned types legally underflow to their extreme maximum).

That said, there's plenty potentially worrisome here, particularly the potential to overreach the size of reversedInput, relying on it being static (thus zero-filled so a free terminator), and the side-effects of modifying a global in the first place (should be an output argument). But the core problem was one of the conditional.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
0

If you want to store contents of array arr in reverse way in global array reversedInput then one way is this, though Why Global Variables Should Be Avoided When Unnecessary?

void reverseString (char arr[]) {
        for (size_t i = 0, j = strlen(arr); j-- > 0 ; i++) { /* change the condition from i<j to j>=0*/
                reversedInput[i] = arr[j]; /* filling in global array in reverse order */
        }
        reversedInput[i] = '\0'; /* terminate with \0 */
}
Achal
  • 11,821
  • 2
  • 15
  • 37
-1

Suppose Input string is "nitendra" and you want o reverse it. You can reverse a string just swapping first character with last character , then second character with second last character and so on.. till (length of string)/2.

#include <iostream>
    #include <algorithm>
    #include <stdio.h>
    using namespace std;
    void reverseString(char arr[100]);
    char reversedInput[100];


    int main() {

        char input[100];


        cout<<"Please enter a string"<<endl;
        cin.getline(input, 100);
        cout<<"You entered "<<input<<endl;

        reverseString(input);
        cout<<"The reverse is "<<input<<endl;

        system("pause");
        return 0;
    }

    void reverseString (char* arr)
    {
        size_t len = std::strlen(arr);

        for(size_t i=0;i<(size_t)(len/2);++i)
        {
            std::swap(arr[i], arr[len-i-1]);
        }
    }
mystic_coder
  • 462
  • 2
  • 10
  • 1
    Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Oct 30 '18 at 16:33