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
|
2 // Using strtok.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 #include <cstring> // prototype for strtok
8 using std::strtok;
9
10 int main()
11 {
12 char sentence[] = "This is a sentence with 7 tokens";
13 char *tokenPtr;
// char *motCherche="komat";
// int compteMot =0;
14
15 cout << "The string to be tokenized is:\n" << sentence
16 << "\n\nThe tokens are:\n\n";
17
18 // begin tokenization of sentence
19 tokenPtr = strtok( sentence, " " );
20
21 // continue tokenizing sentence until tokenPtr becomes NULL
22 while ( tokenPtr != NULL )
23 {
//
24 cout << tokenPtr << '\n';
// if motCherche==tokenPtr {
// compteMot++};
25 tokenPtr = strtok( NULL, " " ); // get next token
26 } // end while
27
28 cout << "\nAfter strtok, sentence = " << sentence << endl;
29 return 0; // indicates successful termination
30 } // end main
The string to be tokenized is:
This is a sentence with 7 tokens
The tokens are:
This
is
a
sentence
with
7
tokens
After strtok, sentence = This |
Partager