Created
January 27, 2021 13:42
-
-
Save arthurbacci/2be9df366095f64c2cad92ffd3d67d37 to your computer and use it in GitHub Desktop.
Replace all substrings in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
char *replace_substring_in_string(char *str, char *from, char *to) | |
{ | |
char *result; | |
unsigned int str_len, from_len, to_len; | |
str_len = strlen(str); | |
from_len = strlen(from); | |
to_len = strlen(to); | |
if (str_len + 1 - from_len <= 0) | |
{ | |
return NULL; | |
} | |
int match_at = -1; | |
for (unsigned int i = 0; i < str_len + 1 - from_len; i++) | |
{ | |
char match = 1; | |
for (unsigned int j = 0; j < from_len; j++) | |
{ | |
if (str[i + j] != from[j]) | |
{ | |
match = 0; | |
break; | |
} | |
} | |
if (match == 0) | |
{ | |
continue; | |
} | |
match_at = i; | |
break; | |
} | |
if (match_at == -1) | |
{ | |
return NULL; | |
} | |
result = malloc(str_len + to_len - from_len + 1); | |
memcpy(result, str, match_at); | |
memcpy(&result[match_at], to, to_len); | |
memcpy(&result[match_at + to_len], &str[match_at + from_len], str_len - match_at - from_len); | |
result[str_len + to_len - from_len] = '\0'; | |
return result; | |
} | |
char *replace_all_substrings_in_string(char *str, char *from, char *to) | |
{ | |
char *result; | |
unsigned int str_len, from_len, to_len; | |
str_len = strlen(str); | |
from_len = strlen(from); | |
to_len = strlen(to); | |
if (str_len + 1 - from_len <= 0) | |
{ | |
return NULL; | |
} | |
int num_matches = 0; | |
for (unsigned int i = 0; i < str_len + 1 - from_len; i++) | |
{ | |
char match = 1; | |
for (unsigned int j = 0; j < from_len; j++) | |
{ | |
if (str[i + j] != from[j]) | |
{ | |
match = 0; | |
break; | |
} | |
} | |
if (match == 0) | |
{ | |
continue; | |
} | |
num_matches++; | |
} | |
if (num_matches == 0) | |
{ | |
return NULL; | |
} | |
int matches_at[num_matches]; | |
unsigned int k = 0; | |
for (unsigned int i = 0; i < str_len + 1 - from_len; i++) | |
{ | |
char match = 1; | |
for (unsigned int j = 0; j < from_len; j++) | |
{ | |
if (str[i + j] != from[j]) | |
{ | |
match = 0; | |
break; | |
} | |
} | |
if (match == 0) | |
{ | |
continue; | |
} | |
matches_at[k++] = i; | |
} | |
result = calloc(str_len + (to_len - from_len) * num_matches + 1, sizeof(char)); | |
int result_len = str_len + (to_len - from_len) * num_matches; | |
unsigned int s = 0; | |
for (unsigned int i = 0; i < num_matches; i++) | |
{ | |
//memcpy(&result[s], &str[s], matches_at[i]); | |
memcpy(&result[matches_at[i] + s], to, to_len); | |
/*memcpy( | |
&result[matches_at[i] + to_len + s], | |
&str[matches_at[i] + from_len + s], | |
str_len - matches_at[i] - from_len | |
);*/ | |
s += to_len - from_len; | |
} | |
//result[s] = '\0'; | |
int offset = 0; | |
for (unsigned int i = 0; i < result_len; i++) | |
{ | |
if (result[i] == '\0') | |
{ | |
result[i] = str[i - offset]; | |
} | |
for (unsigned int j = offset; j < num_matches; j++) | |
{ | |
if (i - offset == matches_at[j]) | |
{ | |
offset += to_len - from_len; | |
break; | |
} | |
} | |
} | |
result[result_len] = '\0'; | |
return result; | |
} | |
int main() | |
{ | |
char *replace_me = "C is a programming language"; | |
char *replace_from = "is a"; | |
char *replace_to = "is the best"; | |
char *str = replace_all_substrings_in_string(replace_me, replace_from, replace_to); | |
printf("%s\n%s\n", replace_me, str); | |
free(str); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment