- 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
I always understood that in C an assignment returned the value assigned, not whether the assignment was successful or not.
eg. if (a=b)
This evaluates to the value of b. essentially, if b is non zero then do something.
This if ((a = b) == 0) as suggested by another poster, wouldn't assign and test for success because the assignment would return the value of b, not a true/false. In fact, (a=b)==0 iff b==0.
This is the basis of C style string manipulation, where strings are null (ie, zero) terminated. I'd probably expect a warning from any decent compiler but it's perfectly valid and useful.
Admin
By the way, FBI traced your IP ( "hackers" the movie style, you know, like 127.282.160.8 ) and is coming at your house.
Admin
When you use recursive operations there is a stack depth you can not exceed and it varies from computer to computer.
http://www.devx.com/tips/Tip/14276
Stack overflow in this example would be caused by strings somewhere around 2000-10000 chars in length.
This isn't a bug with .Net hehe
Justin www.AboutJustin.com
Admin
c++ do have replace function in the standard string class.
Oh right, you don't actually know that.
Admin
Would that work?
Admin
Btw, it's amazing how many of the versions posted here don't work.
Admin
Admin
K, this is the final one.
Admin
I lied.
Admin
God, I'm glad I know regular expressions :)
Admin
VB6 does have a replace function, it is VB5 that had poor in-built string manipulation functions
myvariable = REPLACE (myvariable, " ", "")
Admin
COBOL. I want an implmentation in Cobol. That should be cool.
Admin
Both this, and the original 'reference implementation' suffer from the same fault: they use pointer arithmetic. Relatively harmless in this particular case, but dangerous and inefficient in general.
Once you start modifying pointers you defeat the commpiler's optimiser. I know that K&R, and most programming courses, say to do it this way, but your code will run faster if you use two integer subscripts to index from[] and to[]. Let the compiler convert this to pointer arithmetic: it will do it much more efficiently than you can.
As for safety, if you use a subscript you have something you can debug; if all you have is a pointer, who knows what are valid pointer values and what are not?
Admin
Ick, strlen for no good reason.
Admin
Last one from me, this is the C++ Enterprise Version.
Admin
Hi,
he used NULL instead of 0 hence it definately is C not C++
Admin
It looks like VB code to me, so I tried to do this the hard way:
Sub RemoveSpaces() Dim MyStr As String MyStr = Replace(MyStr, " ", "", 1) End Sub
Oneliner ... :-)
Admin
You do compile with warnings on don't you?
Admin
Dude, C++ is the superset of C, therefore any C program is also a C++ program...
Admin
For the next challenge. Two representations of this routine, one in Brainfuck & one in Whitespace, in the same file, with the programs interleaved. Split both of them up in such a way that the comments - you do comment, don't you? - make sense for both programs.
Admin
Now call despace_in_place(NULL);
Admin
Admin
It's VB.NET, the end.
Admin
In .Net, the preferred way to return a value from a method (or Function in VB.Net) is actually NOT to assign to [FunctionName], but to use "return [value]", as is the case with C# or most other "C"-ish languages.
I suppose backward compatibility brings out the worst in some programmers, though...
Also, a void method is declared as a "Sub" in VB. A "Function" always returns a value, and if no type is specified, "System.Object" is assumed.
Admin
Not all languages are compiled. It's still a useful habit imho.
Admin
Obfuscators of the world, unite:
Admin
Forgot to account for null:
Not tight, but fun :)
Admin
For those few of you that don't read Befunge-93 fluently: the 6-line block in the middle is the main function, which takes a null-terminated string off the stack, and pushes a new string on the stack, without spaces. The first and last lines are a wrapper for testing (the first line puts a string on the stack and jumps to the function; the last line prints the string currently on the stack and terminates).
Tested with http://www.quirkster.com/befunge/befunge.html.
Admin
Wrong. If you do not specify the return type, it defaults to "object".
If Option Strict is on, you are required to specify the return type.
Admin
I agree... .NET is an amazing platform to develop on. In my experience, while VB.NET shouldn't be considered a "toy language", those who develop on it are "toy developers"... Oh, I'm sure there are very good developers that choose to use VB.NET--many of us here just haven't met them yet. =)
Admin
Well sure, but who ever looks at those???
Admin
Admin
You left out the sarcasm tags. In any event, this is not how VB.NET works. The callee sees a copy of the string when it uses the passed variable.
Admin
templates, the other way round ;)
Admin
You have it backwards. If a string is passed ByRef, a REFERENCE is passed -- essentially a pointer to the original.
With ByVal, the VALUE (a copy of the value of a variable) is passed, and you cannot change the original.
With every language, one needs to learn how to use it before attempting.
Admin
I get undefinedundefinedundefinedundefined... as the result no matter what I put in. Windows XP/IE7.
Admin
CynicalTyler is a genius. :-)
Admin
Speaking of O(n) algorithms, why not generate every subset of the string (in the original order) and return the one that is the longest but doesn't contain any spaces?
Admin
Damn, I guess IE7 can save us from WOPR too. My PC's power is toasted, couldn't test :'(
( and, indeed, insert anti-microsoft sarcasm here )
Admin
Here's a Python version that is more dedicated to the subject of this site ;-)
Admin
How about in MUF?
: do-strip explode 1 swap 1 for pop swap strcat repeat ;
: main "this is a string" " " do-strip ;
Admin
This is how I would've done it, only tested in bash: (usage: var=
removeSpaces "string here"
)(captcha: pinball - what this thread is playing with my brain as the ball)
Admin
This was also true in FORTRAN77 FUNCTION subprograms. It's an old idea. Here's a not-quite right F77 version, but it's in the spirt of the exercise. Oddly, it's not as bad a the VB one!
FUNCTION REMOV(ST) CHARACTER*300 REMOV, ST INTEGER LEN
Admin
Maybe the INSPECT verb will work?!? COBOL has formatting built-ins to normalise records.
INSPECT MY-STRING-VALUE REPLACING ALL SPACES BY ''.
Admin
Unicode doesn't have characters, it has codepoints. Unicode encodings have bytes that represent codepoints.
Admin
If that was an old version of VB, then that would be the most condensed version, as there is no way to actually remove spaces from a string in one line, to my knowledge.
Admin
I know!
string& RemoveSpace(string &s) { return while(fork()); }
Admin
Uh, well, okay...
The string in this case is in memory. The function thus takes two extra lines [one to get the argument, one to return the result], but still gets the job done. The rest of the code shown is the standard stuff that has to be in any Perl program, if one is not to be taken to task by the Perly monks.
In all fairness, though, the the regex used has quite a bit of C code under the hood. The advantage in this case is that the underlying code only had to be written once, and the result functionality is available readily, without re-inventing that wheel.
Admin
Coolness. You inspired me. Pascal was the first language I learned--on paper. I never had a chance to use it, though, except under the hood in C++[?] Builder.
I now have another browser window open at: http://sourceforge.net/project/showfiles.php?group_id=2174 and as soon as I get to the coffee shop where I have access to a high-speed connection, I will download and intall it, for the sake of nostalgia.
Admin