Advertisement
Guest User

Copy Paste - Softuni Advanced

a guest
Jun 9th, 2021
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. #include <sstream>
  5.  
  6. std::string copyMethod(int begin, int end, const std::string& input) {
  7.     auto leftindex = input.find_last_of(' ', begin) + 1;
  8.     auto rightIndex = input.find(' ', end);
  9.     if (rightIndex == std::string::npos)
  10.     {
  11.         rightIndex = input.size();
  12.     }
  13.     return  input.substr(leftindex,rightIndex - leftindex);
  14. }
  15.  
  16. void pasteMethod(int pos, std::stack <std::string>& clipBoard, std::string& input) {
  17.     if (!clipBoard.empty()) {
  18.         if (input[pos] == ' ') {
  19.             std::string addingSpaces = ' ' + clipBoard.top();
  20.             input.insert(pos, addingSpaces);
  21.             clipBoard.pop();
  22.         }
  23.         else
  24.         {
  25.             input.insert(pos, clipBoard.top());
  26.         }
  27.     }
  28. }
  29.  
  30. int main()
  31. {
  32.     std::string input;
  33.     getline(std::cin, input);
  34.  
  35.     std::stack <std::string> clipBoard;
  36.     std::string command;
  37.  
  38.     while (getline(std::cin, command)) {
  39.         std::stringstream line(command);
  40.         std::string word;
  41.         line >> word;
  42.         if (word == "end") break;
  43.         else if (word == "copy") {
  44.             int beginPos, endPos;
  45.             line >> beginPos >> endPos;
  46.             clipBoard.push(copyMethod(beginPos, endPos, input));
  47.         }
  48.         else if (word == "paste") {
  49.             int pos;
  50.             line >> pos;
  51.             pasteMethod(pos, clipBoard, input);
  52.         }
  53.     }
  54.  
  55.     std::cout << input;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement