Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void tokenize (char * source, char ** tokens, int & size_of_tokens)
  5. {
  6.     char * token = strtok(source, " ,.!?:-\\\t");
  7.     int size = 0;
  8.     while (token)
  9.     {
  10.         bool found = false;
  11.         for (size_t i=0; i<size; i++)
  12.         {
  13.             if (strcmp(token,tokens[i]) == 0)
  14.                 found = true;  
  15.         }
  16.         if (!found)
  17.         {
  18.             strcpy (tokens[size], token);
  19.             size++;
  20.         }
  21.         token = strtok(NULL, " ,.!?:-\\\t");
  22.     }
  23.     size_of_tokens = size;
  24. }
  25.  
  26. int common_symb_in_order_cnt (char * str1, char * str2, size_t len)
  27. {
  28.     int cnt = 0;
  29.     for (int i=0; i<len; i++)
  30.     {
  31.         if (str1[i]==str2[i])
  32.             cnt++;
  33.     }
  34.     return cnt;
  35. }
  36. void main()
  37. {
  38.     const size_t Max_Tokens = 64;
  39.     const size_t Max_Token_Size = 16;
  40.     char ** tokens = new char * [Max_Tokens];
  41.     for (int i=0; i<Max_Tokens; i++)
  42.     {
  43.         tokens [i] = new char [Max_Token_Size];
  44.     }
  45.     char source[] = "You can cut all the flowers but you can not keep Spring from coming.";
  46.     puts (source);
  47.     cout << endl;
  48.     int size = 0;
  49.     tokinize (source, tokens, size);
  50.  
  51.     for (int i=0; i<size; i++)
  52.     {
  53.         cout << tokens[i] << endl;
  54.     }
  55.     cout << endl;
  56.  
  57.     const size_t len_word=16;
  58.     char * word = new char [len_word];
  59.     cout << "Input word: ";
  60.     cin.getline(word, len_word);
  61.  
  62.     size_t fact_word_len = strlen(word);
  63.     int max_symbol_match = 0;
  64.     char * correction = new char [Max_Token_Size];
  65.     for (int i=0; i<size; i++)
  66.     {
  67.         if (strlen(tokens[i])==fact_word_len)
  68.         {
  69.             int symbol_match = common_symb_in_order_cnt(tokens[i], word, fact_word_len);
  70.             if (symbol_match > max_symbol_match)
  71.             {
  72.                 max_symbol_match = symbol_match;
  73.                 strcpy (correction, tokens[i]);
  74.             }
  75.         }
  76.     }
  77.     cout << correction << endl;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement