`i'm trying to reverse each word in a sentence with the positions of the words fixed. the line while(c!=' ') segfaults each time.Can somebody please let me know where i'm going wrong?here goes my code.
#include<stdio.h>
void swap(char *i, char *j)
{
char t;
t = *i;
*i = *j;
*j = t;
}
void reverse(char *s, char *e)
{
char *i, *j;
i = s;
j = e;
while(i <= j)
{
swap(i, j);
i++;
j--;
}
}
int main()
{
int check = 0;
char *a= (char*) malloc(100*sizeof(char));
char *c, *b, *t;
char *s = ' ';
printf("enter your sentence\n");
fgets (a, 100, stdin);
if ((strlen(a) > 0) && (a[strlen(a)-1] == '\n'))
a[strlen(a)-1] = '\0';
printf("\nyour stat: %s and size is %d\n", a, strlen(a));
b = a;
c = a;
while(*b != ' ')
b++;
b--;
while(!check)
{
reverse(c, b);
t = c;
c = b;
b = t;
while(*c != ' ')// segmentation fault :|
c++;
while(*c == ' ')
c++;
b++;
while(*b == ' ')
b++;
while((*b != ' ') && (*b != '\0'))
{
if(*b = '\0')
{
check = 1;
b--;
reverse(c, b);
break;
}
b++;
}
b--;
}
printf("\n reversed stat is : %s\n",a);
return 0;
}