scanf() and cin tokenize on whitespace, so yes, they stop reading input at any whitespace. Please don't use gets(). fgets() reads an entire line (it even returns the newline character at the end of the line), so if there are spaces in the line you'll get them too. The C++ equivalent is getline():
string s; getline( cin, s );
Except getline() doesn't leave the newline in the string.
There's no standard function to simply remove whitespace. (Are you thinking of things along the lines of removing leading and trailing whitespace, or do you also want to get rid of internal whitespace?)
If you are using C++, I suggest you check out the Boost String Algorithms Library . It has some stuff that might help.
Otherwise, you'll have to write your own function.
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
I just couldn't believe there wasn't one already.
I'm curious. Why would you think there's a function to remove spaces from a string? Of what use would such a function be inthe real world? :?:
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Sigh.
#include <algorithm> // remove_if()
#include <cctype> // isspace()
#include <functional> // ptr_fun <>
#include <iostream> // I/O
#include <string> // string
using namespace std;
int main()
{
cout << "Space remover.\n\n"
"Please enter strings with spaces.\n"
"Enter a blank line to quit.\n\n"
"> "
<< flush;
string s;
while (getline( cin, s ) && !s.empty())
{
s.erase(
remove_if(
s.begin(),
s.end(),
ptr_fun <int, int> ( isspace )
),
s.end()
);
cout << s << endl << string( s.length(), '-' ) << endl;
cout << "\n> " << flush;
}
cout << "Bye.\n";
return 0;
}
See lines 21 through 28. Replace theisspace() function with one of your choosing.
Is this thread one of those that is going to be resurrected once a year forever more?
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
One algorithm candidate is:
input =
[h][e][l][l][o][ ][w][o][r][l][d]
for each char c in input
if c is a space
then shift the contents to the left, removing the space.
Of course it would probably be more efficient to copy the array, assuming it is of a manageable size.
pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
All your suggestions are appreciated, but this thread is 3,5 years old. The original poster is probably no longer interested in it. :icon_smile:
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Is this thread one of those that is going to be resurrected once a year forever more?
Yep.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
Is this thread one of those that is going to be resurrected once a year forever more?
Yep.
Wanna bet? :icon_wink:
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944