#include <stdio.h>
#include <string.h>
void custom_replace(char *str, const char *find, const char *replace) {
int find_len = strlen(find);
int replace_len = strlen(replace);
int str_len = strlen(str);
for (int i = 0; i <= str_len - find_len; i++) {
int j;
for (j = 0; j < find_len; j++) {
if (str[i + j] != find[j]) {
break;
}
}
if (j == find_len) {
memmove(str + i + replace_len, str + i + find_len, str_len - i - find_len + 1);
memcpy(str + i, replace, replace_len);
str_len = str_len - find_len + replace_len;
}
}
}
int main() {
char str[] = "Hello, world! This is a test.";
const char *find = "world";
const char *replace = "universe";
custom_replace(str, find, replace);
printf("Modified string: %s\n", str);
return 0;
}
|