Advertisement
vencinachev

K2Zad3

Dec 15th, 2020
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <cctype>
  3.  
  4. using namespace std;
  5.  
  6. enum State { START, WORD, SPACE };
  7.  
  8. string acronym(string text);
  9.  
  10. int main()
  11. {
  12.     string input;
  13.     cout << "Enter sentence: ";
  14.     getline(cin, input);
  15.     cout << "Acronym: " << acronym(input) << endl;
  16.     return 0;
  17. }
  18.  
  19. string acronym(string text)
  20. {
  21.     State st = START;
  22.     string acr = "";
  23.     if (isalpha(text[0]))
  24.     {
  25.         st = WORD;
  26.         acr += toupper(text[0]);
  27.     }
  28.     else
  29.     {
  30.         st = SPACE;
  31.     }
  32.     for (int i = 1; i <= text.length(); i++)
  33.     {
  34.         if (st == SPACE)
  35.         {
  36.             if (isalpha(text[i]))
  37.             {
  38.                 st = WORD;
  39.                 acr += toupper(text[i]);
  40.             }
  41.         }
  42.         else if (st == WORD)
  43.         {
  44.              if (!isalpha(text[i]))
  45.             {
  46.                 st = SPACE;
  47.             }
  48.         }
  49.     }
  50.     return acr;
  51. }
  52.  
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement