-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringsearch.c
executable file
·284 lines (224 loc) · 5.92 KB
/
stringsearch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <stdlib.h>
#include <stdio.h>
#include <limits.h> // INT_MAX
#include "60lib.c"
// string length
long unsigned int my_strlen(const char* str)
{
char c = 1;
int amt;
if (str == NULL)
return -1;
for (amt = 0; c; amt++)
{
str++;
c = *str;
}
return amt;
}
// compares first len symbols of s1 and s2
int my_strncmp(const char* s1, const char* s2, long unsigned int len)
{
char c1;
char c2;
long unsigned int i;
if (s1 == NULL || s2 == NULL)
return INT_MAX;
c1 = *s1;
c2 = *s2;
for (i = 0; (i < len && (c1 || c2)); i++)
{
if (c1 > c2)
return 1;
else if (c1 < c2)
return -1;
// next char
c1 = *s1;
c2 = *s2;
s1++;
s2++;
}
return 0;
}
// compares s1 and s2
int my_strcmp(const char* s1, const char* s2)
{
return my_strncmp(s1, s2, my_strlen(s1));
}
// first occurence of character c
char* my_strchr(const char* str, int c)
{
const char* p = str;
while (*p)
{
if (*p == c)
return (char* )p;
p++;
}
return NULL;
}
// first occurence of string needle
char* my_strstr(const char* haystack, const char* needle)
{
const char* p = haystack;
int needle_len; // const
if (haystack == NULL || needle == NULL)
return NULL;
needle_len = my_strlen(needle);
while ((p = my_strchr(p, *needle)) != 0)
{
if (my_strncmp(p, needle, needle_len) == 0)
return (char* ) p;
p++;
}
return NULL;
}
// counts needles in the haystack
int str_occurences(char *hay, char *needle)
{
char *ptr = hay;
int occurs = -1;
// count occurences
while (ptr != NULL)
{
occurs++;
ptr = my_strstr(ptr, needle);
// move the pointer to prevent an inf loop
if (ptr != NULL)
ptr++;
}
return occurs;
}
// replace
// shift str to the left by n bytes
void str_shiftl(char *str, int n)
{
int i;
for (i = 0; i < my_strlen(str); i++)
str[i] = str[i + n];
}
// shift str to the right by n bytes
void str_shiftr(char *str, int n)
{
int i;
for (i = my_strlen(str) + n; i >= n; i--)
str[i] = str[i - n];
}
// replace all occurences of needle in *haystack with replace_with,
// allocate more memory if needed.
// requires you to count all occurences of needle beforehand
void replace_str(char **haystack, char *needle, int n_len,
char *replace_with, int rw_len, int occurences)
{
// calculate new and old string length
int str_old_len = my_strlen(*haystack);
int str_new_len = str_old_len + (rw_len - n_len) * occurences;
// allocate memory if needed
if (str_new_len > str_old_len)
{
*haystack = realloc(*haystack, str_new_len);
check_alloc(*haystack, 1);
}
// start replacing
char *ptr_hay = my_strstr(*haystack, needle);
int hay_i;
if (ptr_hay == NULL)
return;
hay_i = (ptr_hay - *haystack);
while (1)
{
// ptr_hay is a part of the string that starts with needle
char *ptr_hay = *haystack + hay_i;
// make room for our replacement. TODO: shift only once
str_shiftl(ptr_hay, n_len);
str_shiftr(ptr_hay, rw_len);
// insert the replacement
for (int i = 0; i < rw_len; i++)
ptr_hay[i] = replace_with[i];
// skip to next occurence
ptr_hay = my_strstr(*haystack, needle);
if (ptr_hay == NULL)
break;
hay_i = (ptr_hay - *haystack);
}
// free unneeded memory
if (str_new_len < str_old_len)
{
*haystack = realloc(*haystack, str_new_len);
check_alloc(*haystack, 1);
}
}
// loops thru all strings and does the replacement if they match the criteria
void replace_str_arr(char **arr, int arr_len, char *needle, int n_len,
char *replace_with, int rw_len, int occurences)
{
for (int i = 0; i < arr_len; i++)
if (str_occurences(arr[i], needle) == occurences)
replace_str(&arr[i], needle, n_len, replace_with, rw_len, occurences);
}
// 2
// what could this possibly do?.. (ʃ⌣́,⌣́ƪ)
void print_str_arr(int len, char **arr)
{
int i = 0;
for (i = 0; i < len; i++)
printf("%s\n", *(arr + i));
}
// searches for strings that have "occurences" occurences of needle and prints them
void search_arr(int arr_len, char **arr, char *needle, int occurences)
{
int i;
for (i = 0; i < arr_len; i++)
if (str_occurences(arr[i], needle) == occurences)
printf("%d: %s\n", i, arr[i]);
}
int main()
{
char **arr = NULL;
int i;
int arr_len;
int occurences;
char *needle = NULL;
char *replace_with = NULL;
//
// INPUT
//
// ask for array length
// to know when to stop reading
printf("Enter array length: ");
arr_len = read_uint();
if (arr_len == -1)
exit(2);
printf("Enter the text:\n");
for (i = 0; i < arr_len; i++)
{
char **ptr;
arr = realloc(arr, sizeof(arr)*(i + 1));
check_alloc(arr, 1);
ptr = arr + i;
*ptr = read_string('\n');
}
// printf("This is the inputted array:\n");
// print_str_arr(arr_len, arr);
printf("Needle: ");
needle = read_string('\n');
printf("Occurences: ");
occurences = read_uint();
printf("Replace with: ");
replace_with = read_string('\n');
//
// OUTPUT
//
// searches arr for strings with occurences occurences of needle and prints them
search_arr(arr_len, arr, needle, occurences);
// silently replaces needle with replace_with for eligible strings
replace_str_arr(arr, arr_len, needle, my_strlen(needle), replace_with,
my_strlen(replace_with), occurences);
// print the changed array
printf("This is the changed array:\n");
print_str_arr(arr_len, arr);
free_str(arr, arr_len);
free(arr);
free(needle);
free(replace_with);
}