-
Notifications
You must be signed in to change notification settings - Fork 5
/
strtok.cpp
40 lines (35 loc) · 828 Bytes
/
strtok.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
#include <cstring> // prototype for strtok
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
char sentence[] = "This is a sentence with 7 tokens";
char *tokenPtr;
std::cout << "The string to be tokensized is:\n"
<< sentence << "\n\nThe tokens are\n"
<< '\n';
// begin tokenization of sentence
tokenPtr = strtok(sentence, " ");
// continue tokenizing sentence until tokenPtr becomes NULL
while (tokenPtr) {
std::cout << tokenPtr << '\n';
tokenPtr = strtok(NULL, " ");
}
std::cout << "\nAfter strtok, sentence = " << sentence << '\n';
return 0;
}
/**
* The string to be tokensized is:
* This is a sentence with 7 tokens
*
* The tokens are
*
* This
* is
* a
* sentence
* with
* 7
* tokens
*
* After strtok, sentence = This
*/