Skip whitespace

Hi Guy's,
I was wondering if there was a string function or cin function to remove all whitespace from a string.

ie convert 1 2 3 4 to 1234

regards
Brendan


Sure. Use the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <algorithm>  // for remove()
#include <iostream>
#include <string>

using namespace std;

int main()
  {
  string s;

  cout << "Please enter a string containing spaces> ";
  getline( cin, s );

  // Here's the STL algorithm to remove spaces (or whatever char you specify)
  remove( s.begin(), s.end(), ' ' );

  cout << "I don't like spaces.\n"
       << s
       << endl;

  return EXIT_SUCCESS;
  }
Last edited on
Topic archived. No new replies allowed.