Advertisement
Taraxacum

Extract words from an article

Oct 28th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #define ALPHA true
  6. #define MISC false
  7.  
  8. char toLower(char ch)
  9. {
  10.     if (ch >= 'A' && ch <= 'Z') {
  11.         return ch - 'A' + 'a';
  12.     } else {
  13.         return ch;
  14.     }
  15. }
  16.  
  17. bool isAlpha(char ch)
  18. {
  19.     return (ch >= 'a' && ch <= 'z') || ch == '\'';
  20. }
  21.  
  22. int main()
  23. {
  24.     char words[256][80] = { '\0' };
  25.  
  26.     int idx = -1;
  27.     int cursor = 0;
  28.     char buf;
  29.     bool state = MISC;
  30.  
  31.     while (cin.get(buf) && buf != '@') {
  32.         char ch = toLower(buf);
  33.         if (isAlpha(ch)) {
  34.             if (state == MISC) {
  35.                 idx++;
  36.             }
  37.             state = ALPHA;
  38.             words[idx][cursor] = ch;
  39.             cursor++;
  40.         } else {
  41.             state = MISC;
  42.             cursor = 0;
  43.         }
  44.     }
  45.  
  46.     for (int i = 0; i <= idx; i++) {
  47.         cout << words[i] << endl;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement