String input

What is the best way to take a string as input ignoring the whitespaces? For example :

If input is : IGNORE WHITE SPACE
string should contain : IGNOREWHITESPACE
Here's two possibilities ... but I prefer the first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
#include<string>
#include<cctype>             // for isspace() etc.
#include<iterator>
#include<algorithm>
using namespace std;

string noWhiteSpace( const string &str )
{
   string result;
   for ( char c : str ) if ( !isspace( c ) ) result += c;
   return result;
}


int main()
{
   string line;
   cout << "Input some text: ";
   getline( cin, line );
   cout << "Without white space: " << noWhiteSpace( line ) << endl;

   cout << "\nAlternative method: type some text and hit enter:\n";
   copy( istream_iterator<char>( cin ), {}, ostream_iterator<char>( cout ) );
}

Last edited on
Is there any way to directly read input from keyboard into a string ignoring white spaces?
You read a string (line 20).
You convert that into a string with no white spaces via noWhiteSpace( line ), as on line 21.
Here, I chose to print it straight out - but you could just reassign it to line as
line = noWhiteSpace( line );

Maybe, you could also use the stream-iterator approach and copy into a string rather than straight to cout. By default it ignores the white spaces.

Beyond that, I don't understand what you want.
Last edited on
How do i use the stream-iterator to copy into a string?
Sorry, @SOURABH PRAKASH PATI, I would have to stick to the noWhiteSpace() function.

The nearest that I can get to with stream iterators is the following. However, it DOESN'T WORK as there is nothing to conclude the cin stream (until you hit CTRL-C).

Can anyone else in the forum advise?

NON-WORKING PROGRAM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<string>
#include<iterator>
#include<algorithm>
using namespace std;

int main()
{
   string line;

   cout << "Input some text: ";
   copy( istream_iterator<char>( cin ), {}, back_inserter( line ) );     // doesn't work properly (requires you to hit CTRL-C)

   cout << "You have just input: " << line << '\n';
}



EDIT:
Well, I did eventually come up with this contrived piece of garbage, but it will unequivocally go down as "Palooka code", so don't use it! I think my original noWhiteSpace() routine was far more sensible.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
#include<string>
#include<iterator>
#include<cctype>
#include<algorithm>
using namespace std;

string noSpaces()
{
   string line;
   find_if( istreambuf_iterator<char>( cin.rdbuf() ), {}, [&line]( char c ){if (!isspace(c)) line+=c; return c=='\n';} );
   cin.get();      // needed to clear the '\n'
   return line;
}

int main()
{
   cout << "Input some text: ";
   string line = noSpaces();
   cout << "You have just input: " << line << '\n';

   cout << "Input some more text: ";
   line = noSpaces();
   cout << "You have now input: " << line << '\n';
}

Input some text: NO WHITE SPACES
You have just input: NOWHITESPACES
Input some more text: no spaces here either
You have now input: nospaceshereeither

Last edited on
If you're wrapping the whole thing in a function anyway, there's no need for that - just hide the getline inside the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>
#include <algorithm>
#include <iostream>
#include <cctype>

// function to return a copy of a string without whitespace
std::string removeWhiteSpace(std::string input) {
    // since we're "removing" things, why not use std::remove_if?
    // EDIT: also make sure to honour the preconditions of std::isspace
    auto new_end = std::remove_if(std::begin(input), std::end(input), 
        [](unsigned char c) { return std::isspace(c); });

    // make sure to resize the string - remove_if doesn't actually modify it!
    input.erase(new_end, std::end(input));

    return input;
}

// wrap our desired usage in a function
std::string getLineNoSpaces() {
    std::string line;
    std::getline(std::cin, line);
    return removeWhiteSpace(line);
}

int main() {
    std::cout << "Input some text: ";
    std::string line = getLineNoSpaces();
    std::cout << "You have just input: " << line << "\n";

    return 0;
}
Last edited on
Another option without getline and removing whitespace.
In case the input is a redirected huge file removing whitespaces might be too slow.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

string ReadStringWithoutWhitespace()
{
  char ch;
  string output;

  while (cin.get(ch))
  {
    if (ch == '\n') // choose different terminating char if necessary
      break;

    if (!isspace(ch))
      output += ch;
  }
  return output;
}
int main() 
{
  string input = ReadStringWithoutWhitespace();
  cout << "Input was: " << input;
  return 0;
}
Registered users can post here. Sign in or register to post.