Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <cctype>
- std::string readInput() {
- std::string line;
- getline(std::cin, line);
- return line;
- }
- std::pair<std::vector<std::string>, std::vector<int>> loadTextInData(const std::string& text) {
- std::pair<std::vector<std::string>, std::vector<int>> data;
- // What this function do:
- /*
- FIRST SECOND
- "Dude" 4
- "," 0
- " " 0
- "what" 4
- " " 0
- "is" 2
- " " 0
- "this" 4
- " " 0
- "nightmare" 9
- " " 0
- "of" 2
- " " 0
- "a" 1
- " " 0
- "task" 4
- "!" 0
- */
- int lettersCount = 0;
- std::string word;
- bool endOfWord = false;
- for (int i = 0; i < text.length(); ++i) {
- if (isalpha(text[i]) || text[i] == '+' || text[i] == char(39)) {
- word.push_back(text[i]);
- lettersCount++;
- endOfWord = true;
- continue;
- }
- if (endOfWord) {
- data.first.push_back(word);
- word = "";
- data.second.push_back(lettersCount);
- endOfWord = false;
- }
- std::string notWordElement;
- notWordElement += text[i];
- data.first.push_back(notWordElement);
- data.second.push_back(0);
- lettersCount = 0;
- }
- return data;
- }
- void handleUpcaseLetters(std::pair<std::vector<std::string>, std::vector<int>>& data) {
- const size_t size = data.first.size();
- for (size_t i = 0; i < size; ++i) {
- std::string currWord = data.first[i];
- const char firstLetter = currWord[0];
- if (i == 0) {
- currWord[0] = char(toupper(firstLetter));
- data.first[i] = currWord;
- continue;
- }
- currWord[0] = tolower(firstLetter);
- data.first[i] = currWord;
- }
- }
- void swapWords(std::pair<std::vector<std::string>, std::vector<int>>& data) {
- const size_t size = data.first.size();
- std::string firstSwapWord;
- std::string sndSwapWord;
- int frontPos = 0;
- int backPos = size - 1;
- while (frontPos != backPos){
- if (backPos == frontPos + 1) { //check if the back counter went thru all words
- frontPos++;
- backPos = size - 1;
- continue;
- }
- if (data.second[backPos] == 0) { //skip if its not word
- backPos--;
- continue;
- }
- if (data.second[frontPos] == 0) { //skip if its not word
- frontPos++;
- continue;
- }
- if (data.second[frontPos] == data.second[backPos]) { //swap words
- std::string tempWordHolder = data.first[frontPos];
- data.first[frontPos] = data.first[backPos];
- data.first[backPos] = tempWordHolder;
- data.second[backPos] = 0; //mark it with 0 so to not appear in search
- frontPos++;
- backPos = size - 1;
- } else {
- backPos--;
- }
- }
- handleUpcaseLetters(data); //deal with the uppercase letters
- }
- std::string createFinalStr(const std::pair<std::vector<std::string>, std::vector<int>>& data) {
- std::string str;
- for (const std::string word : data.first) {
- str += word;
- }
- return str;
- }
- int main()
- {
- std::string text = readInput();
- std::pair<std::vector<std::string>, std::vector<int>> data = loadTextInData(text);
- swapWords(data);
- std::cout << createFinalStr(data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement