Advertisement
DarkoreXOR

Untitled

Oct 6th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7.     std::string input = " know me? do you ";
  8.  
  9.     int count = 0;
  10.  
  11.     //
  12.     // get words
  13.     //
  14.  
  15.     std::vector<std::string> words;
  16.     std::string word;
  17.    
  18.     size_t length = input.length();
  19.    
  20.     for (size_t i = 0; i < length; ++i)
  21.     {
  22.         if (input[i] != ' ' && i != length - 1)
  23.         {
  24.             word += input[i];
  25.         }
  26.         else
  27.         {
  28.             if (word.length() > 0)
  29.             {
  30.                 words.push_back(word);
  31.                 word.clear();
  32.                 count++;
  33.             }
  34.         }
  35.     }
  36.    
  37.     //
  38.     // print words
  39.     //
  40.  
  41.     std::cout << count << std::endl;
  42.    
  43.     for (std::vector<std::string>::iterator it = words.begin(); it != words.end(); it++)
  44.     {
  45.         std::cout << *it << std::endl;
  46.     }
  47.    
  48.     std::cout << "===" << std::endl;
  49.    
  50.     //
  51.     // permutate vector
  52.     //
  53.    
  54.     int firstWord = 2;
  55.     int lastWord = 3;
  56.    
  57.     int repeat = 0;
  58.    
  59.     while (repeat < lastWord - firstWord + 1)
  60.     {
  61.         for (int i = lastWord; i > 0; i--)
  62.         {
  63.             std::swap(words[i], words[i - 1]);
  64.         }
  65.        
  66.         repeat++;
  67.     }
  68.    
  69.     //
  70.     // print permutated vector
  71.     //
  72.    
  73.     for (std::vector<std::string>::iterator it = words.begin(); it != words.end(); it++)
  74.     {
  75.         std::cout << *it << std::endl;
  76.     }
  77.    
  78.     //
  79.     // build & print new string
  80.     //
  81.    
  82.     std::string result;
  83.    
  84.     for (std::vector<std::string>::iterator it = words.begin(); it != words.end(); it++)
  85.     {
  86.         if (it != words.begin())
  87.         {
  88.             result += ' ';
  89.         }
  90.        
  91.         result += *it;
  92.     }
  93.    
  94.     std::cout << "result: " << result << std::endl;
  95.    
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement