-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.c
More file actions
98 lines (96 loc) · 2.04 KB
/
token.c
File metadata and controls
98 lines (96 loc) · 2.04 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *tokenize(char *main_str, char *delimit, char **storage)
{
int breakpoint = 0;
if (*storage != NULL && storage[0][0] == 0)
{
return NULL;
}
if (main_str == NULL)
{
main_str = malloc(100);
strcpy(main_str, *storage);
}
*storage = malloc(100);
int len = strlen(main_str);
int i;
char *retval = NULL;
for (i = 0; i < len; i++)
{
char c = main_str[i];
for (int j = 0; j < strlen(delimit); j++)
{
if (main_str[i] == delimit[j])
{
breakpoint = 1;
retval = malloc(sizeof(100));
strncpy(retval, main_str, i);
break;
}
}
if (retval != NULL)
{
break;
}
}
if (breakpoint == 0)
{
retval = malloc(sizeof(100));
strncpy(retval, main_str, i);
storage[0][0] = 0;
if (strlen(retval) == 0)
{
return NULL;
}
return retval;
}
int found_valid_str = 0;
for (int j = i + 1; j < len; j++)
{
int next_is_valid = 1;
for (int k = 0; k < strlen(delimit); k++)
{
if (main_str[j] == delimit[k])
{
next_is_valid = 0;
break;
}
}
if (next_is_valid == 1)
{
strcpy(*storage, &main_str[j]);
found_valid_str = 1;
break;
}
}
if (found_valid_str == 0)
{
storage[0][0] = 0;
}
if (strlen(retval) == 0)
{
return NULL;
}
if(strlen(*storage) == 0){
*storage = NULL;
}
return retval;
}
// int main()
// {
// char s[10000];
// scanf("%s", s);
// char *str;
// char *stor;
// for (str = s;; str = NULL)
// {
// char *token = tokenize(str, ";:", &stor);
// if (token == NULL)
// {
// break;
// }
// printf("%s\n", token);
// }
// }