- 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
char* RemoveSpaces( char *str ) { if ( NULL == str ) return NULL;
}
Admin
Your implementation is not C++, is plain C.
Admin
Apart from anything else, putting the required code in the Else section....
The've not heard of NOT then.
Admin
Looks like the original code was in VB.NET... How about the following?
string removedSpaces = origString.Replace(" ", String.Empty)
Admin
How about:
new_string = edit$(old_string,2%)
in VAX/DEC BASIC...
Admin
I don't know what toy programming language that first example is (VB? Pascal?), byt surely the "ByVal" means the function is passed a copy of the string, and therefore has no change to the string in the caller?
Admin
In VB / VBA / VBS when you declare a function like this, the function name is also the name of the Variable whose value is returned, in this case the variable RemoveSpace will contain the string that is returned
Admin
Your implementation is not C++, is plain C.
It is C++, just look at the way the conditionals are all the wrong way round, for example:
And the fact that like all C++ programmers he uses NULL despite being told to use 0 by all decent books on the language.
Admin
I'd add :
while ((to) && (*to++ != ' '));
after the assignment of "to", and I'd change "from" to initialize to "to"
Admin
Because they're SO much different.....
Admin
Because C is SO MUCH different than C++ for some tiny little function like this.... C'mon. Seriously
Admin
I'd change that to: char* RemoveSpaces( char *str ) { if ( NULL == str ) return NULL;
}
Admin
My version:
char* RemoveSpaces( char *str ) { return RemoveSpaces(str); }
Why won't it work!?!?! It says remove the spaces!
Admin
Virtually same algorithm, paramaterised the char to strip out, and more c-ified it.
Your c++ one was interesting, in that it used C strings. A real C++ example would be..
Admin
"I don't know what toy programming language that first example is (VB? Pascal?), byt surely the "ByVal" means the function is passed a copy of the string, and therefore has no change to the string in the caller?"
You are correct that the incoming string is a copy but it is returning a string too...
Private Function RemoveSpace(ByVal strFldName As String) As String ... RemoveSpace = RemoveSpace & Mid(strFldName, i, 1) ...
In VB .NET, you "return" a value by setting the method name to a value so long as the method signature ends with AS [type]. If there is no "as [type]" it is essentially a void method.
Admin
Maybe they're one of those paranoids who believes that IF NOT X THEN Y is less exclusive than:
IF X THEN ... ELSE Y
Believe it, I've met people who think that way.
Admin
perl: sub removeSpaces { $_[0] =~ s/ //g; }
haskell: removeSpaces = filter (not . Char.isSpace)
Admin
Perl:
$str =~ s/ //g;
Some things Perl was just meant to do.
Admin
Yes, by design I should think since IT'S A FUNCTION (also look up the term "immutable"), ooooh these pesky "toy" languages can prove tricky can't they. ;-)
Admin
Admin
Admin
So we are having fun of rewriting the function here's my copy
char* RemoveSpaces( char *str ) { if ( NULL == str ) return NULL;
}
Admin
I'm not sure what qualifies a programming language as a "Toy Language..." These days what makes a language "good" is its library support, and I'd say .NET is a nice library to have behind you.
VB.net is not my favorite language, but its a far cry from being a "toy"
Admin
Another case of why C++ will die. Why not add Replace to the standard string class? Is it really that controversial?
Oh right the C++ standards committee doesn't actually do work.
Admin
Since a string in VB.Net is a reference type passing it ByVal is passing the value of the reference. The unmanaged equivalent would be passing a pointer to the string data. If you pass a string ByRef then you are passing a pointer to a pointer to the string data.
Admin
C++:
Admin
Couldn't you use the C function: isalnum(int * a). I think that's how it's called. It would remove all the punctuation from the string too, but who needs punctuation?!
Admin
Why use a regex? $foo =~ tr/ //d is much faster
Admin
if(a = 3) { ... }
Which will compile fine. On the other hand:
if(3 = a) { ... }
Will fail right away. Even as an experienced programmer I occasionally make that mistake.
Admin
Admin
A better one in shell:
This function sets the Env. Var 'RETURN'.
Oh, and we overwrite 'tmp' and 'tmp.c'...
function removeSpaces { cat > tmp.c <<HERE #include <stdio.h> int main(int argc, char **argv) { char *to = argv[1], *from = argv[1]; while( *from != '\0' ) { if( *to != ' ' ) *to++ = *from; from++; } fprintf(stdout, "%s", to); return 0; } HERE
gcc -o tmp tmp.c
RETURN=
./tmp $*
rm tmp tmp.c return [[ -n $RETURN ]] }Admin
C++ string replace functions:
http://www.cppreference.com/cppstring/replace.html
Admin
In a not-so-distant future the definition of NULL or even ZERO could change. I guess this one should be more compatible with future generation systems :
if ( FILENOTFOUND == str ) return FILENOTFOUND;
About the XSLT version : yep, I had to do it myself too, plain nightmare :|
By the way here's the ruby version:
s.gsub!(" ","") or s.gsub!(/ +/,"") ( don't know which one is faster )
edit: this one should be faster : s.tr!(" ","")
Admin
Your code still has a bug in it. Here's the fixed version:
charRemoveSpaces(charstr){return(RemoveSpaces(str));}
Admin
I tried to make this as easy as possible for the JavaScripters out there ;)
Admin
python:
c:
Admin
Why not write it in such a way to make it character-agnostic:
And then call
. Also handy when you want to get rid of something else than spaces :)captcha: sanitarium (leave me beeee....)
Admin
Uhm, I of course mean
. :PAdmin
Of course. It would be a method like String::RemoveSpaces and would use string and not char*
Te fact that most people use C++ as C is because they don't know C++, but rather C with classes.
Admin
Heres a dime, now get yourself a better compiler.
Admin
function removeSpaces(str) var newStr = str.replace(/ /g, //); return newStr; }
Admin
Ah, I was going to ask why the reversed conditional in the original, nifty trick.
Captcha: tastey. Yes in deed. Learning tastes goood (even if it can't spell)
Admin
Derrick's original code really is C++, not C, because the "char * to = str;" line came AFTER the "if( NULL == str )" line, which would fail under C.
also, I'd never write a line quite as heinous as while ((*to) && (to++ != ' ')); Compiler decipher code; humans should never have to. Particularly, when that line could easily have been written: char to = strchr(str, ' ');
But, that bring us to another important point... What about tabs and other forms of whitespace? Has no one heard of isspace()?
const char End_of_String = '\0'; #define NULL 0
char* RemoveSpaces( char *str ) { if ( NULL == str ) return NULL;
}
Admin
Not in C99 if I'm not mistaken.
Admin
Admin
Some Haskell:
removeSpaces = filter (/= ' ')
Admin
Since this code is VB.NET it would just have been string.Replace(" ", "") The try catch is probably there for when the argument is null (Nothing in VB?) so that the function will return an empty string.
Admin
Here's a bad one:
Admin
also:
Admin
I'd like to put forward the first recursive version noted here (obviously this is for the more advanced programmers among us). It works brillantly, but some users report that with long strings they get some kind of stack error (a bug in the C# framework I suspect).