-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_string_delimeter.c
More file actions
76 lines (66 loc) · 1.69 KB
/
split_string_delimeter.c
File metadata and controls
76 lines (66 loc) · 1.69 KB
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
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
bool split_string(char **str_parsen, char *buffer, char delimiter, int *numb_tokens);
int main() {
char string[] = "$GPAAM,A,A,0.10,N,WPTNME*32";
char **tokens= (char**)malloc(sizeof(char *)* (20));
int token;
char delimiter = ','; //',' or '-'
int status;
status = split_string(tokens ,string, delimiter, &token);
printf("status %d\n", status);
for (int i = 0; i < token; i++) {
printf("data split[%d]: %s\n", i, tokens[i]);
}
printf("number of token %d\n",token);
free(tokens);
return 0;
}
/*
Result:
status 1
data split[0]: $GPAAM
data split[1]: A
data split[2]: A
data split[3]: 0.10
data split[4]: N
data split[5]: WPTNME*32
number of token 6
status 0
data split[0]: $GPAAM,A,A,0.10,N,WPTNME*32
number of token 1
*/
/*
brief: split string with delimiter
param tokens: pointer to pointer char token
param buffer: buffer string data
param delimiter: character for split string
param numb_tokens: number of tokens
return: false or true
*/
bool split_string(char **tokens, char *buffer, char delimiter, int *numb_tokens){
bool result = true;
int m=0;
int j=1;
int count = 0;
for(int i=0;i<strlen(buffer);i++){
if(buffer[i] == delimiter){
j++;
}
}
if(j==1){
result = false;
*numb_tokens = j;
tokens[count++] = strdup(buffer);
} else {
*numb_tokens = j;
char *buf = strtok(buffer, &delimiter);
while (buf != NULL) {
tokens[count++] = strdup(buf);
buf = strtok(NULL, &delimiter);
}
}
return result;
}