Advertisement
Guest User

Ultimate Reverse words

a guest
May 12th, 2021
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <cctype>
  5.  
  6. std::string readInput() {
  7.     std::string line;
  8.     getline(std::cin, line);
  9.     return line;
  10. }
  11.  
  12. std::pair<std::vector<std::string>, std::vector<int>> loadTextInData(const std::string& text) {
  13.     std::pair<std::vector<std::string>, std::vector<int>> data;
  14.     // What this function do:
  15.     /*
  16.      FIRST        SECOND
  17.      "Dude"       4
  18.      ","          0
  19.      " "          0
  20.      "what"       4
  21.      " "          0
  22.      "is"         2
  23.      " "          0
  24.      "this"       4
  25.      " "          0
  26.      "nightmare"  9
  27.      " "          0
  28.      "of"         2
  29.      " "          0
  30.      "a"          1
  31.      " "          0
  32.      "task"       4
  33.      "!"          0
  34.      */
  35.     int lettersCount = 0;
  36.     std::string word;
  37.     bool endOfWord = false;
  38.     for (int i = 0; i < text.length(); ++i) {
  39.         if (isalpha(text[i]) || text[i] == '+' || text[i] == char(39)) {
  40.             word.push_back(text[i]);
  41.             lettersCount++;
  42.             endOfWord = true;
  43.             continue;
  44.         }
  45.         if (endOfWord) {
  46.             data.first.push_back(word);
  47.             word = "";
  48.             data.second.push_back(lettersCount);
  49.             endOfWord = false;
  50.         }
  51.         std::string notWordElement;
  52.         notWordElement += text[i];
  53.         data.first.push_back(notWordElement);
  54.         data.second.push_back(0);
  55.         lettersCount = 0;
  56.     }
  57.     return data;
  58. }
  59.  
  60. void handleUpcaseLetters(std::pair<std::vector<std::string>, std::vector<int>>& data) {
  61.     const size_t size = data.first.size();
  62.     for (size_t i = 0; i < size; ++i) {
  63.         std::string currWord = data.first[i];
  64.         const char firstLetter = currWord[0];
  65.         if (i == 0) {
  66.             currWord[0] = char(toupper(firstLetter));
  67.             data.first[i] = currWord;
  68.             continue;
  69.         }
  70.         currWord[0] = tolower(firstLetter);
  71.         data.first[i] = currWord;
  72.     }
  73. }
  74.  
  75. void swapWords(std::pair<std::vector<std::string>, std::vector<int>>& data) {
  76.     const size_t size = data.first.size();
  77.     std::string firstSwapWord;
  78.     std::string sndSwapWord;
  79.     int frontPos = 0;
  80.     int backPos = size - 1;
  81.     while (frontPos != backPos){
  82.         if (backPos == frontPos + 1) { //check if the back counter went thru all words
  83.             frontPos++;
  84.             backPos = size - 1;
  85.             continue;
  86.         }
  87.         if (data.second[backPos] == 0) { //skip if its not word
  88.             backPos--;
  89.             continue;
  90.         }
  91.         if (data.second[frontPos] == 0) { //skip if its not word
  92.             frontPos++;
  93.             continue;
  94.         }
  95.         if (data.second[frontPos] == data.second[backPos]) { //swap words
  96.             std::string tempWordHolder = data.first[frontPos];
  97.             data.first[frontPos] = data.first[backPos];
  98.             data.first[backPos] = tempWordHolder;
  99.             data.second[backPos] = 0; //mark it with 0 so to not appear in search
  100.             frontPos++;
  101.             backPos = size - 1;
  102.         } else {
  103.             backPos--;
  104.         }
  105.     }
  106.     handleUpcaseLetters(data); //deal with the uppercase letters
  107. }
  108.  
  109. std::string createFinalStr(const std::pair<std::vector<std::string>, std::vector<int>>& data) {
  110.     std::string str;
  111.     for (const std::string word : data.first) {
  112.         str += word;
  113.     }
  114.     return str;
  115. }
  116.  
  117. int main()
  118. {
  119.     std::string text = readInput();
  120.     std::pair<std::vector<std::string>, std::vector<int>> data = loadTextInData(text);
  121.     swapWords(data);
  122.     std::cout << createFinalStr(data);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement