19 if (s != NULL && c1 !=
'\0')
21 size_t length = strlen(s);
24 for (
size_t index = 0; index < length; index++)
34 size_t writeIndex = 0;
35 for (
size_t readIndex = 0; readIndex < length; readIndex++)
37 if (s[readIndex] != c1)
39 s[writeIndex] = s[readIndex];
53 if (s != NULL && c1 !=
'\0')
55 size_t length = strlen(s);
56 int upperc1 = toupper(c1);
59 for (
size_t index = 0; index < length; index++)
61 if (toupper(s[index]) == upperc1)
69 size_t writeIndex = 0;
70 for (
size_t readIndex = 0; readIndex < length; readIndex++)
72 if (toupper(s[readIndex]) != upperc1)
74 s[writeIndex] = s[readIndex];
93static void _append_range(
char** buffer,
const char* start,
const char* end)
95 if (buffer != NULL && start < end)
97 size_t charsToCopy = end - start;
101 text = malloc(charsToCopy + 1);
104 memcpy(text, start, charsToCopy);
105 text[charsToCopy] =
'\0';
111 size_t currentLength = strlen(text);
112 char* reallocText = realloc(text, currentLength + charsToCopy + 1);
113 if (reallocText != NULL)
115 memcpy(reallocText + currentLength, start, charsToCopy);
116 reallocText[currentLength + charsToCopy] =
'\0';
117 *buffer = reallocText;
127char*
replace_str(
const char* s,
const char* str1,
const char* str2)
129 char* newText = NULL;
130 if (s != NULL && str1 != NULL && str2 != NULL)
132 size_t length = strlen(s);
133 size_t str1Length = strlen(str1);
134 size_t str2Length = strlen(str2);
135 const char* start = s;
136 const char* end = start + length;
139 const char* found = strstr(start, str1);
147 start = found + str1Length;
165 char* newText = NULL;
166 if (s != NULL && str1 != NULL && str2 != NULL)
168 size_t length = strlen(s);
169 size_t str1Length = strlen(str1);
170 size_t str2Length = strlen(str2);
171 const char* start = s;
172 const char* end = start + length;
175 const char* found =
strstri(start, str1);
183 start = found + str1Length;
Declaration of the Replace() functions, for replacing characters and strings in a string.
Declaration of the strstri function, case-insensitive string search for narrow character strings.
void replace_chri(char *s, char c1, char c2)
Replace all occurrences of narrow character c1 with narrow character c2 in s, using case-insensitive ...
char * replace_str(const char *s, const char *str1, const char *str2)
Replace all occurrences of narrow string str1 with narrow string str2 in s, using case-sensitive sear...
char * replace_stri(const char *s, const char *str1, const char *str2)
Replace all occurrences of narrow string str1 with narrow string str2 in s, using case-insensitive se...
void replace_chr(char *s, char c1, char c2)
Replace all occurrences of narrow character c1 with narrow character c2 in s, using case-sensitive se...
static void _append_range(char **buffer, const char *start, const char *end)
Helper function to append a range of characters to a string that may need to be resized to accommodat...
char * strstri(const char *s1, const char *s2)
Do case-insensitive search for string 2 (s2) in string 1 (s1). Similar to the C library's strstr().