-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmystrings.c
70 lines (60 loc) · 1.17 KB
/
mystrings.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
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
char *remove_spaces(char *s) {
char *p, *q;
p = q = s;
while (*p) {
while ((*p) && (isspace(*p)))
p++;
while ((*p) && (!isspace(*p)))
*q++ = *p++;
}
*q = '\0';
return s;
}
char *copy_without_spaces(char *s) {
s = strdup(s);
if (!s)
return NULL;
return remove_spaces(s);
}
int count_chars(char *s, int c) {
int n;
n = 0;
while (*s)
n += (int)(*s++ == c);
return n;
}
char *str_tolower(char *s) {
char *p;
p = s;
while (*p) {
*p = tolower(*p);
p++;
}
return s;
}
/* NB: alters input - replaces all "c" with '\0' */
char **str_split(char *s, int c) {
char **arr;
int i, n;
n = count_chars(s, c);
if (!(arr = malloc((n + 2) * sizeof(char *))))
return NULL;
for (i = 0; i < n + 1; i++) {
arr[i] = s;
if ((s = strchr(s, c)))
*s++ = '\0';
arr[i + 1] = s;
}
return arr;
}
bool is_all_digits(char *s) {
while (*s) {
if (!isdigit(*s++))
return false;
}
return true;
}