I cannot run this following code since getting segmentation fault. In the main i enter a string and a substring, whereas in the function i catch the substring and remove it from the string using memmove.
#include <stdio.h>
#include <string.h>
void removeSubstring(char *s, const char *r) {
int len = strlen(r);
char *pos;
while ((pos = strstr(s, r)) != NULL) {
memmove(pos, pos + len, strlen(pos + len) + 1);
}
}
int main() {
char *s;
const char *r;
printf("Enter the first string: ");
scanf(" %[^n]s", &s);
printf("Enter the second string: ");
scanf(" %[^n]s", &r);
removeSubstring(s, r);
printf("Modified string: %sn", s);
return 0;
}
i receive a segmentation fault
New contributor
Alberto Mura is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.