DimovIvan

C++Fundamentals-Exam Preparation-Ultimate Reverse words (it’s over 9000)

May 7th, 2021 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. #include<sstream>
  5. #include<cctype>
  6. #include<algorithm>
  7. #include<list>
  8.  
  9. std::vector<std::string> readInput() {
  10.     std::vector<std::string> vec{};
  11.     std::string line;
  12.     getline(std::cin, line);
  13.     std::istringstream istr(line);
  14.     std::string word;
  15.     while (istr >> word)
  16.     {
  17.         vec.push_back(word);
  18.     }
  19.     return vec;
  20. }
  21. void swap(std::string& str1, std::string& str2) {
  22.     std::string oldStr1 = str1;
  23.     str1 = str2;
  24.     str2 = oldStr1;
  25. }
  26. std::vector<std::string> erasePuntuations(std::vector<std::string>& sentence,
  27.                                           std::vector<int>& getPunctIdx,
  28.                                           std::vector<char>& getPuncts) {
  29.     size_t size = sentence.size();
  30.     for (size_t j = 0; j < size; ++j) {
  31.         std::string currWord = sentence[j];
  32.         if (ispunct(currWord[currWord.size() - 1]) && currWord[currWord.size() - 1] != '+')
  33.         {
  34.             getPunctIdx.push_back(j);
  35.             getPuncts.push_back(currWord[currWord.size() - 1]);
  36.             sentence[j].pop_back();
  37.         }
  38.     }
  39.     return sentence;
  40. }
  41. size_t getIndex(std::vector<std::string>& noPunctSentence, std::string& str) {
  42.     std::vector<std::string>::const_iterator it =
  43.         std::find(noPunctSentence.begin(), noPunctSentence.end(), str);
  44.     return it - noPunctSentence.begin();
  45. }
  46. std::vector<std::string> reverseWords(std::vector<std::string>& noPunctSentence) {
  47.     size_t size = noPunctSentence.size();
  48.     int counterswaps = 0;
  49.     for (size_t j = 0; j < size; ++j) {
  50.         bool foundEqualSizeWord = true;
  51.         if (j > 0)
  52.         {
  53.             int counterRepetitions = 0;
  54.             for (int k = j - 1; k >= 0; k--)
  55.             {
  56.                 if (noPunctSentence[j].size() == noPunctSentence[k].size())
  57.                 {
  58.                     counterRepetitions++;
  59.                 }
  60.             }
  61.             if (counterRepetitions != 0)
  62.             {
  63.                 int counterEqualRep = 0;
  64.                 for (int i = size - 1; i >= 0; i--) {
  65.                     if (noPunctSentence[j].size() == noPunctSentence[i].size())
  66.                     {
  67.                         if (j == i)
  68.                         {
  69.                             foundEqualSizeWord = false;
  70.                             break;
  71.                         }
  72.                         counterEqualRep++;
  73.                         if (counterEqualRep == counterRepetitions)
  74.                         {
  75.                             size = getIndex(noPunctSentence, noPunctSentence[i]) - 1;
  76.                             std::cout << "start searching from index: " << size << std::endl;
  77.                             break;
  78.                         }
  79.                     }
  80.                 }
  81.                 if (!foundEqualSizeWord)
  82.                 {
  83.                     continue;
  84.                 }
  85.             }
  86.             else
  87.             {
  88.                 size = noPunctSentence.size();
  89.             }
  90.  
  91.         }
  92.         for (int i = size - 1; i >= 0; i--)
  93.         {
  94.             if (noPunctSentence[j].size() == noPunctSentence[i].size() && i > j)
  95.             {
  96.                 swap(noPunctSentence[j], noPunctSentence[i]);
  97.                 std::cout << noPunctSentence[i] << "<->" << noPunctSentence[j] << std::endl;
  98.                 counterswaps++;
  99.                 break;
  100.             }
  101.         }
  102.         continue;
  103.     }
  104.     std::cout << "swaps: " << counterswaps << std::endl;
  105.     return noPunctSentence;
  106. }
  107. std::vector<std::string> getPunctToReverseSentence(const std::vector<int>& getPunctIdx,
  108.                                                    const std::vector<char>& getPuncts,
  109.                                                    std::vector<std::string>& reverseSentence) {
  110.     size_t size = getPunctIdx.size();
  111.     for (size_t i = 0; i < size; i++)
  112.     {
  113.         reverseSentence[getPunctIdx[i]].push_back(getPuncts[i]);
  114.     }
  115.     return reverseSentence;
  116. }
  117. std::string getPosOfUpperAlpha(std::vector<std::string>& reverseSentenceWithPunct) {
  118.     std::string reverseSentence;
  119.     for (const std::string word : reverseSentenceWithPunct) {
  120.         reverseSentence += ' ' + word;
  121.     }
  122.     reverseSentence = reverseSentence.erase(0, 1);
  123.     for (char& c : reverseSentence) {
  124.         if (isalpha(c) && isupper(c))
  125.         {
  126.             c = tolower(c);
  127.         }
  128.     }
  129.     reverseSentence[0] = toupper(reverseSentence[0]);
  130.     return reverseSentence;
  131. }
  132. //void printSolution(const std::vector<std::string>& solution) {
  133. //  for (const std::string word : solution) {
  134. //      std::cout << word << ' ';
  135. //  }
  136. //  std::cout << std::endl;
  137. //}
  138. int main() {
  139.     std::vector<std::string> sentence = readInput();
  140.     std::vector<int> getPunctIdx;
  141.     std::vector<char> getPuncts;
  142.     std::vector<std::string> noPunctSentence = erasePuntuations(sentence, getPunctIdx, getPuncts);
  143.     std::vector<std::string> reverseSentence = reverseWords(noPunctSentence);
  144.     for (const std::string word : reverseSentence) {
  145.         std::cout << word << ' ';
  146.     }
  147.     std::cout << std::endl;
  148.     for (const int punctIdx : getPunctIdx) {
  149.         std::cout << punctIdx << ' ';
  150.     }
  151.     std::cout << std::endl;
  152.     for (const char c : getPuncts) {
  153.         std::cout << c << ' ';
  154.     }
  155.     std::cout << std::endl;
  156.     std::vector<std::string> reverseSentenceWithPunct = getPunctToReverseSentence(getPunctIdx, getPuncts, reverseSentence);
  157.     for (const std::string word : reverseSentenceWithPunct) {
  158.         std::cout << word << ' ';
  159.     }
  160.     std::cout << std::endl;
  161.     std::string solution = getPosOfUpperAlpha(reverseSentenceWithPunct);
  162.     std::cout << solution << std::endl;
  163.     //printSolution(solution);
  164.     return 0;
  165. }
Add Comment
Please, Sign In to add comment