Something is wrong with this C code to reverse string, but I dont know what ? please help
- by Harbhag
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;
}