1

The code got compiled successfully. but I can't reverse the string. since I am a beginner-level programmer, I can't really figure out the mistake I made.

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

int main() {      
    int temp, i, j, length;
    char name[20], name1[20];

    printf(" Enter string \n");
    scanf("%s", name);
    length = strlen(name);
    printf(" %d", length);
    for (i = length - 1; i >= 0; i--) {
        name1[length - i] = name[i];
    }
    printf("%S ", name1);
    
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    "The code got compiled successfully." Consider using a compiler more beginner-friendly, one that emits warnings for "correct-ish" code. Or turn on the warnings of your current compiler (increase warning level) and **mind those warnings** as if they are errors. – pmg May 24 '21 at 06:50

4 Answers4

1

Use lower case 's' in print:

printf("%s ",name1);

Similar codes: https://www.programmingsimplified.com/c-program-reverse-string

HARSH MITTAL
  • 750
  • 4
  • 17
1

The issues I noticed:

  • length-i will be off by +1 here:
    name1[length-i]=name[i];
    
  • You forgot to add the null terminator (\0).
  • %S is a non-standard printf formatter. You probably want %s.

Suggested changes:

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

int main() {
    char name[20], name1[20];

    printf(" Enter string \n");

    if(scanf("%19s", name) != 1) {         /* check that input works */
        fprintf(stderr, "input failure\n");
        return 1;
    }

    int length = strlen(name);

    printf(" %d\n", length);

    for(int i = 0; i < length; ++i) {
        name1[length - i - 1] = name[i];  /* corrected indexing */
    }

    name1[length] = '\0';                 /* add null terminator */

    printf("%s\n", name1);                /* %s instead of %S */
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

You can try this one:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int i, length;
    char name[20], temp;
    printf(" Enter string \n");
    scanf("%s", name);
    length = strlen(name);
    printf(" %d", length);
    for(i = 0; i < length /2; i++)
    {
        temp = name[i];
        name[i] = name[length-i-1];
        name[length-i-1] = temp;
    }
    printf("\n%s", name);
    return 0;
}
0

Here are some issues in your code:

  • you should limit the number of characters stored into name with %19s.

  • name1[length - i] = name[i]; does not use the correct offset. Try the first iteration: i is length-1 you copy the last character to position length - (length - 1) hence position 1 instead of 0. Use this instead:

      name1[length - i - 1] = name[i];
    
  • you must set a null terminator at the end of the reversed string:

      name1[length] = '\0';
    
  • in the printf() call, %S is incorrect. Use %s instead and append a newline.

Here is a modified version:

#include <stdio.h>

int main() {      
    char name[20], name1[20];
    int i, length;

    printf(" Enter string:\n");
    if (scanf("%19s", name) == 1) {
        length = strlen(name);
        printf("%d\n", length);
        for (i = 0; i < length; i++) {
            name1[length - i - 1] = name[i];
        }
        name1[length] = '\0';
        printf("%s\n", name1);
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • can you explain how this line of code works if (scanf("%19s", name) == 1) – Mindfreak122314 May 24 '21 at 10:39
  • `scanf` conversion`%s` stores a word into the array pointed to by the next argument. `%19s` limits this conversion to a maximum of 19 characters plus a null terminator, which ensures that no buffer overflow occurs as `name` is an array of 10 bytes. `scanf()` returns the number of successful conversions: it should be `1`. `scanf()` would return `EOF` if no word can be converted because the stream is at end of file. Testing `scanf("%19s", name) == 1` ensures that the rest of the code only executes if not at end of file. – chqrlie May 24 '21 at 17:36