| « Prev | Page 1 | Page 2 | Next » |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 09:03
•
by
akatherder
|
|
One of these days, a programming language will include some sort of replace() function.
|
|
thats still not c++ ;-;
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 09:10
•
by
Keith Hackney
(unregistered)
|
|
fist!
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 09:15
•
by
Keith Hackney
(unregistered)
|
|
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 |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 09:30
•
by
GuntherVB
|
Have they tried javascript? captcha: Why use anything when you have javascript.. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 09:35
•
by
Anonymous
(unregistered)
|
|
you mean like in C# or Java
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 09:36
•
by
Anonymous
(unregistered)
|
|
you mean like in C# or Java
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 09:44
•
by
Jimmy
(unregistered)
|
No, the Real WTF is that they didn't use J#. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 09:46
•
by
sir_flexalot
|
|
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!
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 09:54
•
by
Tepid Perl User
(unregistered)
|
Well, s/ //g is good enough for all those sed and perl users out there... Captha : dubya. Dubya Bush? |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:01
•
by
Magus
(unregistered)
|
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. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:04
•
by
Ted
(unregistered)
|
|
my version:
char* RemoveSpaces( char *str ) { if ( NULL == str ) return NULL; char *to = str; char *from = str; do{ if( *str != ' ' ) *to++ = *str; while( *str++ != '\0 ); return str; } |
|
Thank god this is very easy and straightforward in Java:
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:13
•
by
C.M
(unregistered)
|
|
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: Replace(txt, " ", "_") There's enough WTF's in this add-in to fill a whole month of submissions. /C.M |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:14
•
by
Sandpit
(unregistered)
|
|
Use a real language such as VB6:
Replace(myString, " ", "") (Captcha= ewww) |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:30
•
by
SomeCoder
(unregistered)
|
|
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. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:31
•
by
Poltras
(unregistered)
|
|
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. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:38
•
by
ForcedSterilizationsForAll
(unregistered)
|
That only works if you then compare the two strings character by character, making sure to take the case into account. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:44
•
by
TekdOut
(unregistered)
|
|
with the a little help from a quantum computer...this solutuion would take no time at all
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:45
•
by
misha
|
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; } |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:52
•
by
Sgt. Preston
(unregistered)
|
Or use another real language such as VB.NET (in which our WTF is written): myString = myString.Replace(" ","")
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:52
•
by
Nomikos
(unregistered)
|
|
I think I like the Haskell way better:
removeSpaces = filter (/= ' ') |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 10:55
•
by
Sgt. Preston
(unregistered)
|
|
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.
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 11:06
•
by
Monkey
(unregistered)
|
|
Not shorter but more obscure?
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 11:31
•
by
CynicalTyler
(unregistered)
|
|
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 |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 11:42
•
by
AJ
(unregistered)
|
|
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:
char* rmSpaces(char* str) {
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 11:48
•
by
Peter Lawrey
(unregistered)
|
|
Or in Java
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 12:01
•
by
D
(unregistered)
|
Would that even work? What does the following return? removespaces("this has multiple spaces in a row");
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 12:15
•
by
java.lang.Chris;
|
|
How about designing functions that don't restrict you to removing or replacing just one character?
#include <stdio.h> |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 12:16
•
by
Reaver
(unregistered)
|
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 |
|
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(char*target,char*src){if(*src++!=' ')*target++=src[-1];if(*src)removespaces(target,src);else*target=0;} |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 12:44
•
by
FlyboyFred
(unregistered)
|
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; } |
void vacuum(std::string &s) {
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 14:09
•
by
Robert
(unregistered)
|
|
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.
<?php CAPTCHA: howdy RESPONSE: Fine, and you? |
|
> I don't even know what the hell the Try/Catch is supposed
> to be doing 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. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 14:14
•
by
jtwine
|
Not sure if you are serious or not because of the strange kinda-sorta-smiley, but it really is C++... This line: for ( char* from = str ; '\0' != *from ; ++from ) -Will not compile in plain C. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 14:48
•
by
Random832
|
|
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. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 15:17
•
by
Claudiu Saftoiu
(unregistered)
|
|
Python way is a one-liner! Probably for other scripting languages too:
>>> string = "hey i want to remove spaces? kk" >>> removed = "".join(c for c in string if c != ' ') >>> removed 'heyiwanttoremovespaces?kk' or alternatively: >>> filter(lambda x: x!=' ', string) 'heyiwanttoremovespaces?kk' or a faster but even stranger looking way >>> filter(' '.__ne__, string) 'heyiwanttoremovespaces?kk' |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 16:03
•
by
duuuuuude
(unregistered)
|
|
Everyone say it with me now...
"Regular Expressions Are Your Friends!" |
|
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. :(
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 17:28
•
by
DWalker59
|
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. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 17:58
•
by
Aurélien
(unregistered)
|
|
Or even simpler:
removed = string.replace(" ", "") |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 20:01
•
by
Claudiu Saftoiu
(unregistered)
|
|
Lol. Yeah, I guess that would be the fastest, simplest way. Silly me.
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 20:45
•
by
-M-
(unregistered)
|
|
Or...you could do it like real python..
string = 'hey i want to remove spaces? kk' string = string.replace(' ','') |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-11 22:42
•
by
Claudiu Saftoiu
(unregistered)
|
|
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!
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-12 02:48
•
by
kupal
(unregistered)
|
|
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 |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-12 16:09
•
by
SuperBlah
(unregistered)
|
|
Don't try to use this in a multithreaded application if you're using strtok. Also, 'more obscure' does not equal 'better'.
|
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-12 16:14
•
by
Joseph Newton
(unregistered)
|
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. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-12 18:11
•
by
ehird
(unregistered)
|
char *rmchar(char *buf, char *src, char rm) QED. |
Re: Classics Week: Removing Spaces, the Easy Way
2007-05-13 02:50
•
by
Matthew
(unregistered)
|
Hey, it's O(1)! Or would that be O(rand(1))? -matthew |
| « Prev | Page 1 | Page 2 | Next » |