sandeep03edu

Remove trailing and forward spaces

Jun 29th, 2022
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. int main() {
  6.     // your code goes here
  7.     string str = "                     ah  na         a                                ";
  8.     string space = " ";
  9.    
  10.     size_t found = str.find(space);
  11.     while(found!=string::npos){
  12.         if(found==0 || found==str.length()-1){
  13.             int pos =found;
  14.             str.erase(pos, 1);
  15.             found = str.find(space);
  16.         }
  17.         else{
  18.             found = str.find(space, found+1);
  19.         }
  20.     }
  21.    
  22.     cout<<str<<endl;
  23.     cout<<str.length()<<endl;
  24.     return 0;
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment