I was writing a program to reverse an entered string in C and my code is :
#include<stdio.h>
#include<string.h>
void main()
{
int sz;
printf("Enter the size of the string : ");
scanf("%d",&sz);
char str[sz];
gets(str);
printf("Enter the string : \n");
gets(str);
char str1[sz];
int i =0;
--sz;
for(i=0;i<=sz;i++)
{
str1[sz-i]=str[i];
}
printf("%s",str1);
}
well this program is giving an weird output for string sizes 8,9 and 10 for size 8 the reversed string is being printed followed by a space and 2 garbage characters,for size 9 the reversed string is being printed followed by 2 garbage characters and for size 10 the reversed string is being printed by a garbage character and for other string sizes the program is running properly. why is this happening?