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();
}
tr0yspradling
  • 529
  • 1
  • 9
  • 20
  • 1
    Please refer to the an earlier question http://stackoverflow.com/questions/83439/remove-spaces-from-stdstring-in-c – Prafulla Dec 06 '11 at 03: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). – tr0yspradling Dec 06 '11 at 03: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. – Adrian McCarthy Jan 18 '18 at 22:14

5 Answers5

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());
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 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 != ' '; });`. – Adrian McCarthy Jan 18 '18 at 22:56
9

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

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 2
    You mean the getline free function, since it's better, and `input` is a `std::string` anyway. – Benjamin Lindley Dec 06 '11 at 04:52
  • As @BenjaminLindley mentioned, use `istream& getline ( istream& is, string& str, char delim ); istream& getline ( istream& is, string& str );` – BruceAdi Dec 06 '11 at 05:01
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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
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.

Aleksandar
  • 3,558
  • 1
  • 39
  • 42
-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];
        }
    }
}