-1

As you can see in the above mentioned title , I don't know if there is something I am doing wrong . I have just started learning c . Please help me with the code.

#include <stdio.h> //code in c language.
#include <string.h>
int main()
{
    char str[20];
    printf("Enter the string:");
    scanf("%s",&str);
    gets(str);
    printf("The string is: %s \n",str);
    printf("The reverse string is : %s",strrev(str));
    return 0;
}
el Drago
  • 35
  • 5
  • First, it's better if you go through small examples first understanding how array works and input into them. Second, using both `scanf` and `get` together makes no sense. So read up on those, also might want to read why not to use `gets`. – ameyCU Mar 19 '21 at 10:02
  • The reason your `gets` gets skipped is that it consumes the newline left over from the previous `scanf`. Note that the `gets` function is [dangerous](https://stackoverflow.com/q/1694036/3049655) and should not be used. Also, you should remove the ampersand from your `scanf` as the name of an array is converted to a pointer to its first element automatically – Spikatrix Mar 19 '21 at 10:04
  • Thanks for the help. I didn't really knew what gets() does and used it . Thank you – el Drago Mar 19 '21 at 10:11

1 Answers1

0

In order to get the complete string from stdin, its recommended to use fgets like follows:

#include <stdio.h> //code in c language.
#include <string.h>

int main()
{
    char str[20];
    printf("Enter the string:");

    /* strtok removes trailing newline character from fgets() */
    strtok(fgets(str, 20, stdin), "\n");
    printf("The string is: %s \n", str);

    /* error is still present here */
    printf("The reverse string is : %s", strrev(str));
    return 0;
}

The above should answer your question although strrev() is currently causing an error!

Here is a link to a previous stackOverflow question on successfully reversing a string: Reversing a string in C

Criss Hills
  • 189
  • 1
  • 12