Advertisement
bozhilov

Astea Solutions Adjusted String w/o Spaces

Mar 28th, 2022 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #include <vector>
  4. #include <bits/stdc++.h>
  5.  
  6.  
  7. string char_vector_method(string input){   //using vectors
  8.     vector<char> vec;
  9.     int index = 0;
  10.     while(index<input.size()){
  11.         if(!isspace(input.at(index))){
  12.             vec.push_back(input.at(index));
  13.         }
  14.         index++;
  15.     }
  16.     input = "";
  17.     for(int i = 0; i<vec.size(); i++){
  18.         input+=vec.at(i);
  19.     }
  20.     return input;
  21. }
  22.  
  23. string string_count_method(string input){ //stl library
  24.     int lenght = input.size(), counted = count(input.begin(), input.end(), ' ');
  25.     remove(input.begin(), input.end(), ' ');
  26.     input.resize(lenght - counted);
  27.     return input;
  28. }
  29.  
  30. int main(){
  31.     string input;
  32.     getline(cin, input);
  33.     cout<<"Char arr method: "<<char_vector_method(input)<<endl;
  34.     cout<<"Stl method: "<<string_count_method(input)<<endl;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement