Advertisement
kasper_k

bs

Nov 25th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdlib.h>
  4. #include <fstream>
  5. #include <string>
  6. #include <sstream>
  7. using namespace std;
  8. string removeSpaces(string s);
  9. int main()
  10. {
  11.     fstream fin("text1.txt", ios::in | ios::out);
  12.     //string a;
  13.     if (!fin)
  14.     {
  15.         cout << "Can't open file" << endl;
  16.     }
  17.     stringstream strs;
  18.     strs << fin.rdbuf();
  19.     fin.seekp(0);
  20.     string line;
  21.     while (getline(strs, line))
  22.     {
  23.         if (!line.empty())
  24.         {
  25.             line = removeSpaces(line);
  26.             fin << line << std::endl;
  27.         }
  28.     }
  29.     fin.close();
  30.     system("pause");
  31.     return 0;
  32. }
  33. string removeSpaces(string s)
  34. {
  35.     for (int j = 0; j < s.length(); j++)
  36.     {
  37.         if (s[j] == ' ')
  38.         {
  39.             while (s[j + 1] == ' ') s.erase(j + 1, 1);
  40.         }
  41.     }
  42.     return s;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement