I'm writing a little programme to remove a word from a string. C segfaults in the for-loop inside the removeWord function (the third printf is never executed). What can the reason be? I'm an absolute newbee in C. When I try to print str[j] as %s inside the for loop, the compiler complains about str being an integer, not a string. Why is that?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void removeWord(char * str, char * toRemove)
{
int i, j, k, stringLen, toRemoveLen;
stringLen = strlen(str); // Length of string
toRemoveLen = strlen(toRemove); // Length of word to remove
// ...(code for finding the word)...
printf ("str='%s' StrLen=%d ToRem=%d i=%d j=%d\n",str, stringLen, toRemoveLen, i, j);
for(j=i; j<=stringLen - toRemoveLen; j++)
{
printf ("j=%d\n", j);
str[j] = str[j + toRemoveLen];
}
printf ("i=%d j=%d\n", i, j);
}
int main(void)
{
char * term = "from the ";
removeWord(term, "from");
return 0;
}