Design Pattern Examples
Overview of object-oriented design patterns
replace.c
Go to the documentation of this file.
1
5
6#include <ctype.h>
7#include <stdlib.h>
8#include <string.h>
9#include <stdio.h>
10
11#include "replace.h"
12#include "strstri.h"
13
15// replace_chr()
17void replace_chr(char* s, char c1, char c2)
18{
19 if (s != NULL && c1 != '\0')
20 {
21 size_t length = strlen(s);
22 if (c2 != '\0')
23 {
24 for (size_t index = 0; index < length; index++)
25 {
26 if (s[index] == c1)
27 {
28 s[index] = c2;
29 }
30 }
31 }
32 else
33 {
34 size_t writeIndex = 0;
35 for (size_t readIndex = 0; readIndex < length; readIndex++)
36 {
37 if (s[readIndex] != c1)
38 {
39 s[writeIndex] = s[readIndex];
40 writeIndex++;
41 }
42 }
43 s[writeIndex] = '\0';
44 }
45 }
46}
47
49// replace_chri()
51void replace_chri(char* s, char c1, char c2)
52{
53 if (s != NULL && c1 != '\0')
54 {
55 size_t length = strlen(s);
56 int upperc1 = toupper(c1);
57 if (c2 != '\0')
58 {
59 for (size_t index = 0; index < length; index++)
60 {
61 if (toupper(s[index]) == upperc1)
62 {
63 s[index] = c2;
64 }
65 }
66 }
67 else
68 {
69 size_t writeIndex = 0;
70 for (size_t readIndex = 0; readIndex < length; readIndex++)
71 {
72 if (toupper(s[readIndex]) != upperc1)
73 {
74 s[writeIndex] = s[readIndex];
75 writeIndex++;
76 }
77 }
78 s[writeIndex] = '\0';
79 }
80 }
81}
82
93static void _append_range(char** buffer, const char* start, const char* end)
94{
95 if (buffer != NULL && start < end)
96 {
97 size_t charsToCopy = end - start;
98 char* text = *buffer;
99 if (text == NULL)
100 {
101 text = malloc(charsToCopy + 1);
102 if (text != NULL)
103 {
104 memcpy(text, start, charsToCopy);
105 text[charsToCopy] = '\0';
106 *buffer = text;
107 }
108 }
109 else
110 {
111 size_t currentLength = strlen(text);
112 char* reallocText = realloc(text, currentLength + charsToCopy + 1);
113 if (reallocText != NULL)
114 {
115 memcpy(reallocText + currentLength, start, charsToCopy);
116 reallocText[currentLength + charsToCopy] = '\0';
117 *buffer = reallocText;
118 }
119 }
120 }
121}
122
123
125// replace_str()
127char* replace_str(const char* s, const char* str1, const char* str2)
128{
129 char* newText = NULL;
130 if (s != NULL && str1 != NULL && str2 != NULL)
131 {
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;
137 while (start < end)
138 {
139 const char* found = strstr(start, str1);
140 if (found != NULL)
141 {
142 _append_range(&newText, start, found);
143 if (str2Length > 0)
144 {
145 _append_range(&newText, str2, str2 + str2Length);
146 }
147 start = found + str1Length;
148 }
149 else
150 {
151 _append_range(&newText, start, start + strlen(start));
152 start = end;
153 }
154 }
155 }
156
157 return newText;
158}
159
161// replace_stri()
163char* replace_stri(const char* s, const char* str1, const char* str2)
164{
165 char* newText = NULL;
166 if (s != NULL && str1 != NULL && str2 != NULL)
167 {
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;
173 while (start < end)
174 {
175 const char* found = strstri(start, str1);
176 if (found != NULL)
177 {
178 _append_range(&newText, start, found);
179 if (str2Length > 0)
180 {
181 _append_range(&newText, str2, str2 + str2Length);
182 }
183 start = found + str1Length;
184 }
185 else
186 {
187 _append_range(&newText, start, start + strlen(start));
188 start = end;
189 }
190 }
191 }
192
193 return newText;
194}
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 ...
Definition: replace.c:51
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...
Definition: replace.c:127
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...
Definition: replace.c:163
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...
Definition: replace.c:17
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...
Definition: replace.c:93
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().
Definition: strstri.c:35