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
| int sentoken(char* sentence, char* token,int pos) { int cur = pos; while (sentence[cur] == ' ') { cur++; }
if (sentence[cur] != '\0') { int count = 0; while (sentence[cur] != '\0' && sentence[cur] != ' ') { token[count] = sentence[cur]; cur++; count++; } token[count] = '\0'; return cur; } else return -1; }
int main() { char sentence[1000]; char token[20]; gets(sentence); int pos=0;
while ((pos = sentoken(sentence, token, pos)) != -1) { printf("%s\n", token); } }
|