Advertisement
Niktiaksk

Untitled

Oct 18th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     string str = "", strTemp = "";
  9.     int counter = 0;
  10.     vector<int> numbersOfLetters;
  11.     vector<string> words;
  12.     cout << "input string :" << endl;
  13.     getline(cin, str);
  14.     int count = 0, sideCount = 0;
  15.     for (char ch : str)
  16.     {
  17.         if (ch != ' ')
  18.         {
  19.             strTemp += ch;
  20.             counter++;
  21.         }
  22.         else
  23.         {
  24.             numbersOfLetters.push_back(counter);
  25.             counter = 0;
  26.             words.push_back(strTemp);
  27.             strTemp = "";
  28.         }
  29.     }
  30.     numbersOfLetters.push_back(counter);
  31.     counter = 0;
  32.     words.push_back(strTemp);
  33.     strTemp = "";
  34.     int maxAmount = numbersOfLetters[0];
  35.     int minAmount = numbersOfLetters[0];
  36.     int posMin = 0, posMax = 0;
  37.     for (int i = 0; i < numbersOfLetters.size(); i++)
  38.     {
  39.         if (numbersOfLetters[i] > maxAmount)
  40.         {
  41.             maxAmount = numbersOfLetters[i];
  42.             posMax = i;
  43.         }
  44.         if (numbersOfLetters[i] <= minAmount)
  45.         {
  46.             minAmount = numbersOfLetters[i];
  47.             posMin = i;
  48.         }
  49.     }
  50.     swap(words[posMin], words[posMax]);
  51.     cout << "swapped string:" << endl;
  52.     for (string s : words)
  53.     {
  54.         cout << s << " ";
  55.     }
  56.  
  57.     return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement