- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
One of these days, a programming language will include some sort of replace() function.
Admin
thats still not c++ ;-;
Admin
fist!
Admin
dup!
Everyone knows the best way to remove spaces is to take a screenshot of the string, print it out, physically cut the spaces out using a scalpel, reassemble the rest of the string, place it on a wooden table, take a photo of it and then scan in the photo.
The Real WTF is that they didn't use PHP and XML.
CAPTCHA = comb-over
Admin
Have they tried javascript?
captcha: Why use anything when you have javascript..
Admin
you mean like in C# or Java
Admin
you mean like in C# or Java
Admin
No, the Real WTF is that they didn't use J#.
Admin
I like generating random strings until you get one that's the same as your starting string but without the spaces. Takes a long time, but eventually it will work perfectly!
Admin
Well,
is good enough for all those sed and perl users out there...
Captha : dubya. Dubya Bush?
Admin
That's assuming your test that one string equals another string with space removed works correctly. You could make an error, such as testing that the two strings are equal ignoring spaces.
Admin
my version:
char* RemoveSpaces( char *str ) { if ( NULL == str ) return NULL;
}
Admin
Thank god this is very easy and straightforward in Java:
Admin
I was called in to fix an old Excel add-in written in VBA. It's the worst code I've ever seen...
Example: Two functions used everywhere in the code to change change " " to "_" or the other way around.
I chose to replace it with this instead:
There's enough WTF's in this add-in to fill a whole month of submissions. /C.M
Admin
Use a real language such as VB6:
Replace(myString, " ", "")
(Captcha= ewww)
Admin
The STL C++ way:
string str = "I have spaces to remove"; string newStr = ""; newStr.resize(str.length()); remove_copy_if(str.begin(), str.end(), newStr.begin(), bind2nd(equal_to<char>(), ' '));
// newStr == "Ihavespacestoremove"
There's probably some other ways using the STL too.
Admin
Here's another unit-tested version (which works, although might not should... or whatever)... Please note the C standard lib notations.
BTW, I think there is a better (read shorter and more obscure) way to make the while loop, but I don't have much time.
captcha: darwin. Give some of those to the ppl who write here without knowing what they talk about.
Admin
That only works if you then compare the two strings character by character, making sure to take the case into account.
Admin
with the a little help from a quantum computer...this solutuion would take no time at all
Admin
How do you then generate thestringwithoutspaces for the comparision in the termination condition? There must be a neat recursive solution here...
sub strip_spaces { my $s = shift;
my $r; while ($r = generate_random_string() ne strip_spaces($s)) {}; return $r; }
Admin
Admin
I think I like the Haskell way better:
Admin
Casting my mind all the way back to February 22nd has me feeling all nostalgic. We should have Classics Week at least once a month.
Admin
Not shorter but more obscure?
Admin
I still prefer feeding my spaces to demonic forces to get rid of them...
http://worsethanfailure.com/Comments/Removing_Spaces,_the_Easy_Way.aspx?pg=2#121986
Admin
The source for this version is longer than both the WTF "reference" and Monkey's version (and unlike Ted's version it actually compiles), but g++ generates fewer machine instructions for it:
Admin
Or in Java
Admin
Admin
How about designing functions that don't restrict you to removing or replacing just one character?
Admin
Typical.
While Willy and George are dicking around with their 'tokens', Paula is getting the job done on her own.
Bet the guys try and take the credit as well...
;->
Cheers, Reaver
Admin
void removespaces(char *target, char *src) { if(*src && *src++ != ' ') *target++ = src[-1]; if(*src) removespaces(target,src); }
elegantly bizarre.
Addendum (2007-05-11 12:50): oops. forgot "else*target=0". also, the "*src &&" in the first condition is not strictly necessary
void removespaces(chartarget,charsrc){if(*src++!=' ')*target++=src[-1];if(src)removespaces(target,src);elsetarget=0;}
Admin
Much easier:
string str = "I have spaces to remove"; str.erase(remove(str.begin(), str.end(), ' '), str.end());
Or for char arrays:
char* removeSpaces(char* buf) { int len = strlen(buf); std::remove(buf, buf+len+1, ' '); return buf; }
Admin
Admin
I am so glad I know PHP and can do this so much better in it. If only they would come up with some sort of str_replace() function though... Oh well.
CAPTCHA: howdy RESPONSE: Fine, and you?
Admin
Well, in most languages string operations like MID/Substring cause the allocation of a new string, hence memory allocation, which can fail raising an exception... :P
Seriously, this is one of the things most quickly forgotten by C++ MFC developers that do know how CString objects really work.
Admin
This line:
for ( char* from = str ; '\0' != *from ; ++from )
-Will not compile in plain C.
Admin
Assuming it's followed by a statement or a block, why not?
Or are you still using the c89 standard? That's so... 1989.
incidentally, "'\0' !=" is a no-op.
for(char *from=str;*from;++from) { ... }
perfectly valid c99.
Admin
Python way is a one-liner! Probably for other scripting languages too:
or alternatively:
or a faster but even stranger looking way
Admin
Everyone say it with me now...
"Regular Expressions Are Your Friends!"
Admin
Dammit, in both occurrences of this article, I posted my vacuum method, and not once has anyone commented how clever my name for it was. :(
Admin
Wort sorting algorithm ever: Generate all possible permuations of the input, and return the one that is sorted.
OR, keep randomly generating permutations of the input, and return when the one you hit on, happens to be sorted.
Admin
Or even simpler: removed = string.replace(" ", "")
Admin
Lol. Yeah, I guess that would be the fastest, simplest way. Silly me.
Admin
Or...you could do it like real python.. string = 'hey i want to remove spaces? kk' string = string.replace(' ','')
Admin
in my goal of finding a quick one-line way to do it, i forgot str had a replace function. I promise, my python code is better than that usually!
Admin
the real wtf is that he should've used regular expression to find the space then have an character array that stores the non-space characters then converting it to a string when finished parsing.
captcha: putanginamo
Admin
Don't try to use this in a multithreaded application if you're using strtok. Also, 'more obscure' does not equal 'better'.
Admin
Aside from the use of literals o, n, and c where old_char new_char, and gone_char would do better, this is the clearest solution I've seen on this thread. I'd note also that C does allow the use of parameter names in function declarations, and that using meaningful names would make those declarations self-commenting.
Admin
QED.
Admin
Hey, it's O(1)!
Or would that be O(rand(1))?
-matthew