9

I have this code to remove whitespace in a std::string and it removes all characters after the space. So if I have "abc def" it only returns "abc". How do I get it to go from "abc def ghi" to "abcdefghi"?

#include<iostream> #include<algorithm> #include<string> int main(int argc, char* argv[]) { std::string input, output; std::getline(std::cin, input); for(int i = 0; i < input.length(); i++) { if(input[i] == ' ') { continue; } else { output += input[i]; } } std::cout << output; std::cin.ignore(); } 
3
  • 1
    Please refer to the an earlier question stackoverflow.com/questions/83439/… Commented Dec 6, 2011 at 3:18
  • 2
    That's using STL algorithms or boost. I'm wanting to do this "by hand". With the way I have written (if possible). Commented Dec 6, 2011 at 3:19
  • Note that the edit fixes the code so the problem in the question is no longer reproducible as described. Thus this question is no longer about removing white space, so I'm voting to close. Commented Jan 18, 2018 at 22:14

5 Answers 5

13

Well the actual problem you had was mentioned by others regarding the cin >> But you can use the below code for removing the white spaces from the string:

str.erase(remove(str.begin(),str.end(),' '),str.end()); 
Sign up to request clarification or add additional context in comments.

1 Comment

That's for removing the spaces from the string in-place. If you want a copy without the spaces (as in the OP), you can use std::copy_if(input.begin(), input.end(), std::back_inserter(output), [](char ch) { return ch != ' '; });.
9

The issue is that cin >> input only reads until the first space. Use getline() instead. (Thanks, @BenjaminLindley!)

2 Comments

You mean the getline free function, since it's better, and input is a std::string anyway.
As @BenjaminLindley mentioned, use istream& getline ( istream& is, string& str, char delim ); istream& getline ( istream& is, string& str );
3

Since the >> operator skips whitespace anyway, you can do something like:

while (std::cin>>input) std::cout << input; 

This, however, will copy the entire file (with whitespace removed) rather than just one line.

Comments

1

My function for removing a character is called "conv":

#include <cstdlib> #include <iostream> #include <string> using namespace std; string conv(string first, char chr) { string ret,s="x"; for (int i=0;i<first.length();i++) { if (first[i]!=chr) s=s+first[i]; } first=s; first.erase(0,1); ret=first; return ret; } int main() { string two,casper="testestestest"; const char x='t'; cout<<conv(casper,x); system("PAUSE"); return 0; } 

You need to change the const char x to ' ' (whitespace, blanco) for the job to be done. Hope this helps.

2 Comments

please use english in your code... This is why C++ keywords are in english, to be globally understood...
@user2007447 that was truly an onld
-1
ifstream ifs(filename); string str, output; vector<string> map; while (getline(ifs, str, ' ')) { map.push_back(str); } for(int i=0; i < map.size();i++){ string dataString = map[i]; for(int j=0; j < dataString.length(); j++){ if(isspace(dataString[j])){ continue; } else{ output +=dataString[j]; } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.