-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.cpp
73 lines (63 loc) · 1.47 KB
/
split.cpp
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
#include <stdio.h>
#include <string.h>
char **split(const char *str,char split,int &size){
char c;
int length = strlen(str);
char *temp = new char[length];
int temp_start = 0;
int i = 0;
size = 0;
char **result = new char *[length];
while(c = str[i++]){
if(c!=split){
temp[temp_start++] = c;
}
if(c==split || i==length){
//find a word
if(i-2>=0 && str[i-2]!=split){
temp[temp_start] = '\0';
//add to result
result[size] = new char[temp_start+1];
strcpy(result[size],temp);
++size;
temp_start = 0;
}
}
}
return result;
}
char **split2(const char *str,char split,int &size){
char c;
int length = strlen(str);
char *temp = new char[length];
int temp_start = 0;
int i = 0;
size = 0;
char **result = new char *[length];
while(c = str[i]){
if(c!=split){
temp_start = 0;
while(c && c!=split){
temp[temp_start++] = c;
c = str[++i];
}
temp[temp_start] = '\0';
result[size] = new char[temp_start+1];
strcpy(result[size],temp);
++size;
}
else{
++i;
}
}
return result;
}
int main(int argc,char *argv[]){
int size = 0;
char **result = split2(" new and delete cannot resize, because they allocate just enough memory to hold an object of the given type. The size of a given type will never change. There are new[] and delete[] but there's hardly ever a reason to use them. ",' ',size);
printf("%d\n",size);
int i = 0;
for(i=0; i<size;++i){
printf("%s\n",result[i]);
}
}