I have the following code. I initialized 2 pointers, one at the beginning and another at the ending of a string. After every step, I increment the 1st pointer and decrement the second pointer. I copy the value of first pointer into the second if the value obtained by dereferencing 1st pointer is less than the value obtained by dereferencing 2nd pointer.
#include <stdio.h>
#include <string.h>
int main() {
char *word="aacdbc";
char *p=word;
char *q=word+(strlen(word)-1);
printf("\n%s\n",word);
int i;
for(i=1;i<=strlen(word)-1;++i) {
if(*p<*q) {
*q=*p;
}
++p;
--q;
}
printf("\n%s\n",word);
return 0;
}
But the code shows a "Segmentation fault error". In which line did I make a mistake?