Something is wrong with this C code to reverse string, but I dont know what ? please help

Posted by Harbhag on Stack Overflow See other posts from Stack Overflow or by Harbhag
Published on 2011-01-10T11:35:17Z Indexed on 2011/01/10 11:53 UTC
Read the original article Hit count: 396

Filed under:
|
|
|

Hi all, I am beginnner to programming. I wrote this little program to reverse a string. But if I try to reverse a string which is less than 5 characters long then it gives wrong output. I cant seem to find whats wrong.

#include<stdio.h>
#include<string.h>
int main()
{
 char test[50];
 char rtest[50];
 int i, j=0;
 printf("Enter string : ");
 scanf("%s", test); 
 int max = strlen(test) - 1;
 for ( i = max; i>=0; i--)
 {
  rtest[j] = test[i];
  j++;
 }
 printf("Reversal is : %s\n", rtest); 

 return 0;
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about string