Removing All Whitespace From a String

Started by
13 comments, last by hughiecoles 16 years, 4 months ago
Quote:Original post by Splinter of Chaos
I think I like godsenddeath's solution the best right now.


I don't. An extra buffer is unnecessary. Using char pointers in favour of real C++ string objects is a bad habit to get into. It only checks for space characters, which is not the same as isspace (which counts: single space, tab, vertical tab, form feed, carriage return, or newline).

Quote:
Yes, the problem can be solved in one line, but is the goal here to solve Celephix's problem, or teach him?


Both. He can use the simpler ones for reference, but in practise use the one liners.

Its like std::vector (or any container), sane people avoid looking at the implementation. When we are learning, we write our own version so we can understand what goes on inside the Standard Library Containers. When writing code, we do not use our hand-rolled version.


Advertisement
Quote:Original post by SiCrane
Quote:Original post by Antheus
While it's possible that some optimizations are done in some algorithms of the standard library, this might be algorithmically optimal:
*** Source Snippet Removed ***

Congratulations, you just rewrote std::remove_if().


After checking the source, I really did. Although the MVS implementation is a bit more clever.

Like I said, in-place copy will be algorithmically optimal. It's good to know that standard implementation takes all such aspects into consideration, making those "performance" concerns really a moot point.
Quote:Original post by rip-off
Quote:
Yes, the problem can be solved in one line, but is the goal here to solve Celephix's problem, or teach him?


Both. He can use the simpler ones for reference, but in practise use the one liners.


Fair enough, but this means that whenever someone wants to post a one liner, s/he should also include a multi-lined code equivalent and an explanation of why they work the same and how the multi-lined one works and how the one liner works in comparison.
Quote:Original post by dalleboy
Go with raz0rs implementation or:

str.erase(std::remove_if(str.begin(), str.end(), isspace), str.end());


Winner.

(Antheus, std::remove and std::remove_if do handle this.)

Here's a more "creative" solution - still linear running time but I suspect it will be a few times slower:

istringstream iss(str);ostringstream oss;string word;while (iss >> word) { oss << word; }str = oss.str();


(Same basic idea as godsenddeath had, but more C++-idiomatic and simpler, and also checking all whitespace.)
Quote:Original post by Splinter of Chaos
I think I like godsenddeath's solution the best right now.

Yes, the problem can be solved in one line, but is the goal here to solve Celephix's problem, or teach him? I like godsenddeath's solution because it's simple and thus shows HOW the problem is resolved. The problems that solve it in one line of code are way too complex (in my opinion) for someone who's confused enough already.


thanks :) as of 8 months ago i had no idea what C++ let alone how to program using it, and i respect the critisim from the following posts, i'm completly interested in constructive critisim, it helps me grow as a programmer

ps. i just figured using char*(aka char[]) would be more universal and proper than using c++ strings, although i could re-write it using std::string, as i would normally would, because personally i only use char[] when it's required by an aPI to avoid .c_str() and other complications
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried

This topic is closed to new replies.

Advertisement