1

I know how to input a text and skip white spaces, but how about when it's a string? During the code I get a string with something like

asdasd asdads asdasd asdasd

and so I need to remove all space in it. So is there some kind of way to write skipws(string_text);?

1
  • What do you mean "remove"? Do you want to construct a string with the spaces spliced out, or do you want to ignore them while iterating over it? Commented Apr 15, 2012 at 11:26

2 Answers 2

3

If you want to construct a string with the value asdasdasdadsasdasdasdasd, you can use the remove-erase idiom:

str.erase( std::remove( str.begin(), str.end(), ' ' ), str.end() );

or using ::isspace from <ctype.h>,

str.erase( std::remove_if( str.begin(), str.end(), ::isspace ), str.end() );
Sign up to request clarification or add additional context in comments.

Comments

0

There is no general way. You could use find/replace in tandem to do that or you could use a stringstream (initialized with your input string) and use >> to get the words without the whitespace and concatenate the results into (another/the same) string

4 Comments

what replace function are you referring to?
@Attila That would be O(N^2).
You are right. I guess that leaves stingstream :) I like your approach better anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.