Okay so I just want to know why my program keeps crashing, all im trying to do is loop through the pointer to a char array and replace the old character with the new one and return how many times I have replaced it:
int main(void) {
char *s = "hello";
printf("%lu\n",str_replace_all(s,'e','f'));
printf("%s",s);
return 0;
}
size_t str_replace_all(char *s,int oldc,int newc) {
size_t count = 0;
for(;*s != '\0'; s++) {
if(*s == oldc) {
*s = newc;
count++
}
}
return count;
}
This compiles fine with gcc -ansi -W -Wall -pedantic
but when i run it it crashes with this:
Segmentation fault (core dumped)
I need help figuring out whats going on, Thanks!
P.S expected output was hfllo