forked from cassianokc/gcshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_util.c
46 lines (43 loc) · 1.15 KB
/
string_util.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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
int read_word(const char *string_full, char **string_piece, char delim) {
static size_t count1 = 0;
int count2 = 0, word_lenght = 0;
while (string_full[word_lenght] != delim
&& string_full[word_lenght] != '\0') {
word_lenght++;
}
*string_piece = malloc(word_lenght * sizeof (char));
if ((*string_piece) == NULL) {
exit(EXIT_FAILURE);
}
while (string_full[count1] != delim) {
if (string_full[count1] == '\0') {
count1 = 0;
(*string_piece)[count2] = '\0';
return FAILURE;
}
(*string_piece)[count2] = string_full[count1];
count1++;
count2++;
}
count1++;
(*string_piece)[count2] = '\0';
return SUCESS;
}
int
count_words(const char *string, char delim) {
size_t count1 = 0;
int delim_count = 1;
while (string[count1] != '\0') {
if (string[count1] == delim) {
if (count1 != 0)
if (string[count1 - 1] != delim)
delim_count++;
}
count1++;
}
return delim_count;
}