Tvor0zhok

Строки III

Dec 9th, 2020 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. //функция считывает слово из строки,
  6. //номер n указывает на начало слова,
  7. //после выхода из функции - на конец слова
  8. string read_word(string s, int &i)
  9. {
  10. //в слове могут быть только буквы и дефис
  11. string word;
  12. while (isalpha(s[i]) || s[i] == '-') { word += s[i]; ++i; }
  13. return word;
  14. }
  15.  
  16. int main()
  17. {
  18. string s;
  19. getline(cin, s);
  20.  
  21. for (int i = 0; i < s.size(); ++i)
  22. if (isalpha(s[i]))
  23. {
  24. string word = read_word(s, i); word[0] = (char)tolower(word[0]);
  25. bool c = 0; //0 - повторяющихся слов не нашлось, 1 - противный случай
  26.  
  27. for (int j = i; j < s.size(); ++j)
  28. if (isalpha(s[j]))
  29. {
  30. string w = read_word(s, j); w[0] = (char)tolower(w[0]);
  31. if (w == word) { s.erase(j - w.size() , w.size() ); c = 1; }
  32. }
  33.  
  34. if (c) s.erase( i - word.size() , word.size() );
  35. i -= word.size();
  36. }
  37.  
  38. cout << s << endl;
  39. return 0;
  40. }
  41.  
  42. //input1: One, two, Three, four, Five, six, Two, Four, Six, three, six.
  43. //output1: One, , , , Five, , , , , , .
  44.  
  45. //input2: Hello, world! Hello, world, again...
  46. //output2: , ! , , again...
Add Comment
Please, Sign In to add comment