Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. using namespace std;
  4.  
  5. void main()
  6. {
  7.     const size_t MAX_TOKENS = 64;
  8.     const size_t MAX_TOKEN_SIZE = 16;
  9.     char ** tokens = new char*[MAX_TOKENS];
  10.     for (int i = 0; i < MAX_TOKENS; i++)
  11.     {
  12.         tokens[i] = new char[MAX_TOKEN_SIZE];
  13.     }
  14.     int *tokens_count = new int[MAX_TOKENS];
  15.     char source[] = "To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token. The scan also stops if the terminating null character is found.";
  16.     char *token = strtok(source," ,.!?");
  17.     int count = 0;
  18.     while (token)
  19.     {
  20.         bool found = false;
  21.         for (size_t i = 0; i < count; i++)
  22.         {
  23.             if (strcmp(token,tokens[i])==0)
  24.             {
  25.                 found = true;
  26.                 tokens_count[i]++;
  27.             }
  28.         }
  29.         if (!found)
  30.         {
  31.             strcpy(tokens[count],token);
  32.             tokens_count[count] = 1;
  33.             count++;
  34.         }
  35.         token = strtok(NULL," ,.!?");
  36.     }
  37.  
  38.     for (int i = 0; i < count; i++)
  39.     {
  40.         cout << "Token = <" << tokens[i] << "> count = " << tokens_count[i] << endl;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement