Apparcane

С++. Видалення зайового. Зробити програму, що видаляє зайві пробіли із рядка що буде введено користу

Dec 9th, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. string removeExtraSpaces(const string& input) {
  6.     string result;
  7.     bool spaceFound = false;
  8.  
  9.     for (char c : input) {
  10.         if (c != ' ') {
  11.             result += c;
  12.             spaceFound = false;
  13.         } else if (!spaceFound) {
  14.             result += c;
  15.             spaceFound = true;
  16.         }
  17.     }
  18.  
  19.     return result;
  20. }
  21.  
  22. int main() {
  23.     string str;
  24.     cout << "Введіть рядок: ";
  25.     getline(cin, str);
  26.  
  27.     string cleanedStr = removeExtraSpaces(str);
  28.     cout << "Рядок після видалення зайвих пробілів: \"" << cleanedStr << "\"" << endl;
  29.     return 0;
  30. }
  31.  
Tags: C++
Advertisement
Add Comment
Please, Sign In to add comment