0
#include <stdio.h>

#include <string.h>

main() {
    int i;
    char given[30];
    char reversed[30];
    printf("enter your stringn");
    scanf("%s", given);
    for (i = 0; i <= (strlen(given)); i++) {
        if (i <= (strlen(given) - 1)) {
            reversed[i] = given[(strlen(given) - 1 - i)];
        } else {
            reversed[i] = given[i];
        }
    }
    printf("%s", reversed);
    return 0;
}

When I run this code, if I give input as 123 its giving back 321 but when I give 123 325 as input it is giving just 321 as output (it should be 523 321). Why is this so?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    When you debug your program, and add this line after the `scanf` line: `printf("The entered string is: %s\n", given);`. You will see that the value of `given` is different than you expect it to be. – Luuk Jun 29 '23 at 15:21
  • more info: [Reading string from input with space character?](https://stackoverflow.com/questions/6282198/reading-string-from-input-with-space-character) – Luuk Jun 29 '23 at 15:22
  • Note that `main()` is only valid in C90. In C99 or later, you must specify the return type ([`int`](https://stackoverflow.com/q/204476/15168), and it is generally preferable to specify `int main(void)` when the command line arguments (though that will be less compelling once C23 is standard). As you're learning C, you should not be coding to the archaic C90 standard; you should be coding to at least C99 and preferably C11 (or C18, which is almost the same as C11). – Jonathan Leffler Jun 29 '23 at 15:32
  • Tip: It makes no sense to call strlen so many times. For a string of length 10, you call it 33 times! Once would suffice. /// Also, don't contort the loop into handling the NUL. Handle that separately. It will greatly simplify the code. – ikegami Jun 29 '23 at 16:31
  • You may want to read this: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) -- If you had checked the result of the line `scanf("%s", given);`, for example by printing the string `given` immediately after executing that line, then you would have discovered that it was that line that was failing, and it would not have been necessary to additionally post the code which reverses the string. Questions seeking debugging help should generally post a [mre] of the problem, instead of posting the entire code. This yields higher quality questions. – Andreas Wenzel Jun 29 '23 at 17:32

2 Answers2

2

%s does not read a whole line. Take a look here. What the entry for %s says is Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.

Maybe How do you allow spaces to be entered using scanf? or How to read one whole line from a text file using <? can point you to alternatives.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
2

For starters the function main shall have explicitly specified return type int (or compatible)

int main( void )

As for your question then according to the C Standard (7.21.6.2 The fscanf function; the same is valid for scanf)

s Matches a sequence of non-white-space characters.

That is as soon as a white space character is encountered filling of the corresponding character array used as an argument expression is stopped. The read sequence of non white space characters is ended with a null character '\0'. Thus only the first word 123 from user input is read and reversed by the program, the rest of the input line would be read by another call to scanf() if you had a loop.

If you want to read strings that contain embedded white space characters, then you need to use another conversion specification as for example:

scanf(" %29[^\n]", given);

Pay attention too that calling the function strlen several times is inefficient.

Also you should declare variables in minimum scopes where they are used.

Your program can look the following way

#include <stdio.h>
#include <string.h>

int main( void ) 
{
    char given[30];
    char reversed[30];

    printf( "enter your string: " );
    
    if ( scanf(" %29[^\n]", given) == 1 )
    {
        size_t n = strlen( given );
        reversed[n] = '\0';

        for ( size_t i = 0; i < n; i++ ) 
        {
            reversed[i] = given[n - 1 - i];
        }

        puts( reversed );
    }

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335