0

I am trying to capture a user input string, then display that string in reverse order next to the initial string. My code is as follows:

char str[300], revstring[300];
int i, strlen;



int main(void) {


    printf("Enter a string: ");                 //Prompt user for input string
    gets(str);

    for (i = 0; str[i] != NULL; i++) {          //Get length of string
        strlen += 1;
    }

    for (i = 0; i <= strlen; i++) {
        revstring[i] = str[strlen - i];
    }

    printf("\n\nThe palindrome of your input is %s%s\n\n\n", str, revstring);

    return 0;
}

When I run the program however, I see nothing after the initial string. I come from a python background so maybe I am thinking about this in too much of a python mindset, but I feel like this should work.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Froobyflake
  • 43
  • 1
  • 8
  • 1
    You copy the null byte ending the first string into the zeroth byte of the second, so what do you suppose gets printed? (Incidentally, both `i` and `strlen` variables should be local variables, not globals — defined inside `main()`, not outside — and the same is probably true of the arrays, too. And using the standard library function name `strlen` as a variable is treading on thin ice. It works, but only just.) – Jonathan Leffler Nov 12 '16 at 22:32
  • `str[i] != NULL` is a poor comparison - `NULL` is a pointer value, not a `char` value. Should be `str[i] != '\0'` – Weather Vane Nov 12 '16 at 22:34
  • Wow I had a feeling it was something like this. I forgot that strings end in a null character. Thank you! – Froobyflake Nov 12 '16 at 22:35
  • Incidentally, please see [Why `gets()` is too dangerous to be used — ever!](http://stackoverflow.com/questions/1694036/) — it is no longer a part of standard C and that is a good thing. – Jonathan Leffler Nov 12 '16 at 22:44

2 Answers2

1

The string is a null-terminated string. You are copying the null character to the beginning of the reversed string. This tells the system that they string is terminated at the first character.

You could use this code instead.

for (i = 0; i < strlen; i++)
{
    revstring[i] = str[(strlen - 1) - i];
}
revstring[strlen] = 0;

Here only the characters before the null character are copied and then the null character is added at the end.

  • Do you need the `-1` when assigning the null byte? (Would it be better to use `'\0'` instead of `0`?) – Jonathan Leffler Nov 12 '16 at 22:37
  • @JonathanLeffler the -1 is necessary as arrays are zero indexed. You could definitely use `'\0'` instead of `0` but it would not make a difference. – Emmanuel Mathi-Amorim Nov 12 '16 at 22:41
  • `i < strlen` is easier to read than `i <= (strlen - 1)` and a fraction quicker. – Weather Vane Nov 12 '16 at 22:47
  • 1
    It had crossed my mind that arrays are zero indexed, but `str[strlen]` is the null byte in the input. You set `revstring[strlen-1]` to null. When you use your code on the input `abcdef`, the output is `abcdeffedcb` which is signally missing the `a`, because your code went trampling on it instead of putting the null byte in the correct place. – Jonathan Leffler Nov 12 '16 at 22:47
  • So I gathered, even when it was gently pointed out to you. It is a good idea to test your code, especially if someone asks a question about it. Actually, it's a good idea to test it anyway — speaking from painful expefrience accumulated over a number of years here on SO. – Jonathan Leffler Nov 12 '16 at 22:50
1

After this loop

for (i = 0; str[i] != NULL; i++) {          //Get length of string
    strlen += 1;
}

str[strlen] is equal to the terminating zero '\0'. And the next loop starts from writing this zero in the first element of the array revstring when i is equal to 0.

for (i = 0; i <= strlen; i++) {
    revstring[i] = str[strlen - i];
}

As result nothing is displayed.

Also you should not forget to append the result string with the terminating zero.

Take into account that the function gets is unsafe and is not supported any more by the C Standard. It is better to use the standard function fgets. But using it you should remove the appended new line character.

The program can be written the

#include <stdio.h>

#define N   300

int main( void ) 
{
    char str[N], revstring[N];

    printf( "Enter a string: " );

    fgets( str, N, stdin );

    size_t  length = 0;
    while ( str[length] != '\0' && str[length] != '\n' ) ++length;

    if ( str[length] == '\n' ) str[length] = '\0';

    size_t i = 0;
    for ( ; i != length; i++ ) revstring[i] = str[length - i - 1];

    revstring[i] = '\0';

    printf("\n\nThe palindrome of your input is %s%s\n\n\n", str, revstring);

    return 0;
}

Its output might look like

Enter a string: Hello, Froobyflake

The palindrome of your input is Hello, FroobyflakeekalfyboorF ,olleH
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • @Froobyflake I am sorry. There was a typo in my program. There must be if ( str[length] == '\n' ) str[length] = '\0'; that is str[length] = '\0' – Vlad from Moscow Nov 12 '16 at 23:20