-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.c
404 lines (339 loc) · 9.06 KB
/
strings.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* memchr
* memcmp
* memcpy
* memmove
* memset
*
* strcat
* strncat
* strchr
* strcmp
* strncmp
* strcoll
* strcpy
* strncpy
* strcspn
* strerror
* strlen
* strpbrk
* strrchr
* strspn
* strstr
* strtok
* strxfrm
*/
#include <stdio.h>
// void *memcpy (void *__restrict __dest, const void *__restrict __src, size_t __n)
void *memcpy(void *dest, const void *src, size_t n) {
int i;
char *cdest = (char *)dest;
const char *csrc = (char *)src;
for (i = 0; i < n; i++) {
cdest[i] = csrc[i];
}
return dest;
}
// void *memmove (void *__dest, const void *__src, size_t __n)
void *memmove(void *dest, const void *src, size_t n) {
char *cdest = (char *)dest;
const char *csrc = (char *)src;
// not checking for overlap here so I could run the risk
// that `csrc` gets overwritten while trying to read from it
// this check only looks to see if the `csrc` starts
// before `cdest` starts. they could still overlap
if (csrc < cdest) {
// start each pointer over by n
for (cdest += n, csrc += n; n--;) {
// go right
*--cdest = *--csrc;
}
} else {
while (n--) {
// go left
*cdest++ = *csrc++;
}
}
return dest;
}
// void *memset (void *__s, int __c, size_t __n)
void *memset(void *src, int c, size_t n) {
char *csrc = (char *)src;
while (n > 0) {
*csrc = (char) c;
csrc++;
n--;
}
return src;
}
// void *memchr (void *__s, int __c, size_t __n)
void *memchr(void *src, int c, size_t n) {
char *csrc = (char *)src;
while (n > 0) {
if (*csrc == (char)c) {
return csrc;
} else {
csrc++;
n--;
}
}
}
// int memcmp (const void *__s1, const void *__s2, size_t __n)
int memcmp(const void *s1, const void *s2, size_t n) {
char *cs1 = (char *)s1;
char *cs2 = (char *)s2;
int diff = 0;
while (n > 0) {
diff += *cs1++ - *cs2++;
n--;
}
return diff;
}
// size_t strlen (const char *__s)
size_t strlen(const char *str) {
size_t len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
// char *strcpy (char *__restrict __dest, const char *__restrict __src)
char *strcpy(char *dest, const char *src) {
size_t slen = strlen(src);
size_t dlen = strlen(dest);
int i;
if (dest == NULL) {
printf("ERR: destination string is NULL and cannot be used\n");
return NULL;
}
if (dlen <= slen) {
printf("ERR: dst '%s' with size '%zu' will not contain\
src '%s' with size '%zu'\n", dest, dlen, src, slen);
return dest;
}
for (i = 0; src[i] != '\0'; i++) {
dest[i] = src[i];
}
dest[i] = '\0';
return dest;
}
// char *strncpy (char *__restrict __dest, const char *__restrict __src, size_t __n)
char *strncpy(char *dest, const char *src, size_t n) {
size_t slen = strlen(src);
size_t dlen = strlen(dest);
int i;
if (dest == NULL) {
printf("ERR: destination string is NULL and cannot be used\n");
return NULL;
}
if (dlen <= slen) {
printf("WARN: truncating dst '%s' with size '%zu' will not contain\
src '%s' with size '%zu'\n", dest, dlen, src, slen);
return dest;
}
for (i = 0; src[i] != '\0' && i <= n; i++) {
dest[i] = src[i];
}
dest[i] = '\0';
return dest;
}
// char *strcat (char *__restrict __dest, const char *__restrict __src)
char *strcat(char *s1, const char *s2) {
size_t len = strlen(s1);
int i;
for (i = 0; s2[i] != '\0'; i++) {
s1[len + i] = s2[i];
}
s1[len + i] = '\0';
return s1;
}
// char *strncat (char *__restrict __dest, const char *__restrict __src, size_t __n)
char *strncat(char *s1, char *s2, size_t n) {
size_t len = strlen(s1);
int i;
for (i = 0; s2[i] != '\0' && i < n; i++) {
s1[len + i] = s2[i];
}
s1[len + i] = '\0';
return s1;
}
// char *strchr (char *__s, int __c)
char *strchr(char *s, int c) {
int i = 0;
while (s[i] != '\0') {
if (s[i] == c) {
return &s[i];
}
i++;
}
return NULL;
}
// int strcmp (const char *__s1, const char *__s2)
int strcmp(const char *s1, const char *s2) {
int i = 0;
int diff = 0;
while (s1[i] == s2[i]) {
i++;
}
diff = s1[i] - s2[i];
return diff;
}
// int strncmp (const char *__s1, const char *__s2, size_t __n)
int strncmp(const char *s1, const char *s2, size_t n) {
int i = 0;
int diff = 0;
while (s1[i] == s2[i] && i < n) {
i++;
}
diff = s1[i] - s2[i];
return diff;
}
// size_t strcspn (const char *__s, const char *__reject)
size_t strcspn(const char *s, const char *reject) {
size_t j = 0;
int k = 0;
while (s[j] != '\0') {
while (reject[k] != '\0') {
if (s[j] == reject[k]) {
return j + 1;
}
k++;
}
k = 0;
j++;
}
return j + 1;
}
// char *strpbrk (char *__s, const char *__accept)
char *strpbrk(char *s, char *accept) {
int i = 0;
int j = 0;
while (s[i] != '\0') {
while (accept[j] != '\0') {
if (s[i] == accept[j]) {
return s[i];
}
j++;
}
j = 0;
i++;
}
return s[i];
}
// size_t strspn (const char *__s, const char *__accept)
size_t strspn(const char *s, const char *accept) {
size_t i = 0;
while ((s[i] != '\0' || accept[i] != '\0') && (s[i] == accept[i])) {
i++;
}
return i;
}
// char *strstr (char *__haystack, const char *__needle)
char *strstr(char *haystack, const char *needle) {
int i = 0;
int j = 0;
int k = 0;
size_t len = strlen(needle);
while (haystack[i] != '\0') {
// beginning of possible matching substring
if (haystack[i] == needle[j]) {
k = i;
while (haystack[k] == needle[j]) {
k++;
j++;
}
}
if (needle[j] == '\0') {
return &haystack[i];
}
k = 0;
j = 0;
i++;
}
return NULL;
}
// char *strtok (char *__restrict __s, const char *__restrict __delim)
char *strtok(char *s, const char *delim) {
// loops through s, checking each char by looping through delim
}
int main() {
char *myName = "grant";
char myBuffer[25];
memcpy(myBuffer, myName, sizeof(myName));
printf("%s is my name\n", myBuffer);
// move 3 bytes from *str into *str + 4
// *str + 4 ' bbb'
// |
// *str 'aaa bbb'
// | |
char asAndBs[] = "aaa bbb";
memmove(asAndBs + 4, asAndBs, 3);
printf("moving A's over B's by moving the first 3 bytes 'aaa' over on top of b's 4 bytes over -> %s\n", asAndBs);
char initial[25] = "this is original";
memset(initial, 'x', 4);
printf("memsetting 4 bytes to 'x' -> %s\n", initial);
char addr[30] = "1234 Elm St. Madison WI";
char *at = memchr(addr, '.', strlen(addr));
printf("found '.' at %s\n", at);
char buff1[20] = "abcdABCD";
char buff2[10] = "abcdabcd";
int difference = memcmp(buff2, buff1, 10);
printf("%s compared to %s is %d\n", buff1, buff2, difference);
char *str = "grant";
printf("input -> '%s' len -> %zu\n", str, strlen(str));
char d[] = "ok this is the end";
printf("src -> '%s'\nattempting copy to '%s'\n", str, d);
strcpy(d, str);
printf("dest -> '%s'\n", d);
char e[] = "holly is my last name";
size_t nChars = 5;
printf("src -> '%s'\nattempting copy of '%zu' chars to '%s'\n", str, nChars, e);
strncpy(e, str, nChars);
printf("%s\n", e);
char str1[] = "grant";
char str2[] = " holly";
printf("concatenating '%s' and '%s'\n", str1, str2);
strcat(str1, str2);
printf("'%s'\n", str1);
char name[] = "carrie";
char lname[] = " holly";
size_t nChar = 4;
printf("contatenating '%s' and '%s' limited to %zu chars\n", name, lname, nChar);
strncat(name, lname, nChar);
printf("'%s'\n", name);
char searchString[] = "search this string";
char ch = 't';
printf("searching '%s' for first '%c'\n", searchString, ch);
char *found = strchr(searchString, ch);
printf("found first '%c' in string '%s'\n", ch, searchString);
printf("the remainder of '%s' at cahr '%c' is %s\n", searchString, ch, found);
char *as = "aaa";
char *bs = "bbb";
int strdiff;
strdiff = strcmp(as, bs);
printf("comparing '%s' and '%s' resulting in a diff of '%d'\n", as, bs, strdiff);
strdiff = strcmp(as, as);
printf("comparing '%s' and '%s' resulting in a diff of '%d'\n", as, as, strdiff);
strdiff = strcmp(bs, as);
printf("comparing '%s' and '%s' resulting in a diff of '%d'\n", bs, as, strdiff);
size_t strncmpChars = 2;
strdiff = strncmp(as, bs, strncmpChars);
printf("comparing '%s' and '%s' up to '%zu' chars resulting in a diff of '%d'\n", as, bs, strncmpChars, strdiff);
strdiff = strncmp(as, as, strncmpChars);
printf("comparing '%s' and '%s' up to '%zu' chars resulting in a diff of '%d'\n", as, as, strncmpChars, strdiff);
strdiff = strncmp(bs, as, strncmpChars);
printf("comparing '%s' and '%s' up to '%zu' chars resulting in a diff of '%d'\n", as, bs, strncmpChars, strdiff);
char *check = "this might contain one or more stop characters";
char *stopChars = "az";
size_t unmatched;
unmatched = strcspn(check, stopChars);
printf("found stop char from '%s' in '%s' and length of unmatched chars is %zu\n", stopChars, check, unmatched);
char *scanThis = "this is the main string to scan";
char *matchThis = "this is the second string";
size_t matched;
matched = strspn(scanThis, matchThis);
printf("'%s' matched '%s' to %zu chars\n", scanThis, matchThis, matched);
char *here = "zzzzzzzzzzzzzzrrr";
char *there = "rr";
printf("searching for first substring '%s' in '%s' and found start at '%s'\n", there, here, strstr(here, there));
}