I'm practicing C language and just built a simple program that inverts sequence of alphabets in a word.
#include <stdio.h>
int main(void)
{
char str[50];
printf("Enter a word: ");
scanf("%s", str);
int i, len=0;
char temp;
while(str[len]!='\0')
{
len++;
}
/*
for(i=0; i<len/2; i++)
{
temp=str[i];
str[i]=str[(len-i)-1];
str[(len-i)-1]=temp;
}
*/
while(i!=len-1)
{
temp=str[i];
str[i]=str[len-1];
str[len-1]=temp;
i++;
len--;
}
printf("%s\n", str);
return 0;
}
I got two versions and one is working very well but the other makes "Segmentation error: 11" I guess I might have accessed wrong memory position but it's quite tricky to figure out what have I done wrong.