- 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
The C# 3.0 way of doing it (string.Replace is for wimps)
Admin
Nice Idea to use filter(). But I prefer: string.replace(" ", "")
Admin
It's just typical newbie code. I'm sure i've written something like than back in the days.
Admin
php
$str = str_replace(' ','',$str);
Admin
I agree. Although I use the same parameter names in the library which contains the originals of these functions, the header file has Doxygen comments that clearly describe each parameters purpose.
Admin
That's seriously broken. Firstly, "str" is not declared, I assume you meant "src". You never increment i, but always increment j - twice if you do the assignment. Even if you change the for statement to increment i rather than j, you don't null terminate the destination array. A fixed version would be:
I suggest you clear your desk and remember to hand in your pass to security as you leave the building.
Admin
TMTOWTDI:
$str =~ y/ //d;
(And this just might be a little more efficient, depending on how Perl optimises the single space RE.
Captcha: kungfu. (How'bout perl-fu? Or bar-fu?)
-Sin Tax
Admin
Admin
/* erm, da-it-da-erm, dah blah erm oort oort oort / char rm0x20s(char *O) { return(O)?{char *o,*Oo;for(*o=*Oo=O;*Oo;(0x20!=*Oo)?*o++=*Oo++:*Oo++);*o=0;O;}:O; }
Admin
Nice memory leak on that benchmark C++ code.
Admin
Nah. Could be worse: generate all possible permutations of the input into a balanced binary tree, then do a depth-first traversal, removing any that are not sorted. If the resulting tree is empty, return "-1" (error); if it contains more than one node, return "-2" (another error); otherwise, return the root node.
(The subtle defect comes at no extra charge.)
Admin
I find it perpetually amusing that Perl is declared to be a write-only language by people who profess any other language under the sun, and then proceed to demonstrate the sheer simplicity and beauty of Perl's power by giving examples in their favorite language; the examples being this inscrutable, non-intelligable gibberish of odd syntax. There is beauty in simplicity. There is tremendous power in simplicity. There is great speed (programmer speed) in simplicity.
$string = "something with spacess I dont care how many are there"; @array = m/(\S+)/g; # capture all non-white space (e.g. tokenize) $string2 = $string; $string2 =~ s/\s+/ /g; # replace all multiple white spaces with a single space $string =~ s/\s+//g; # remove all white space from string
Adding some printfs to this, and running it in Perl printf "string: %s\n",$string; printf "string2: %s\n",$string2;
perl x.pl string: somethingwithspacessIdontcarehowmanyarethere string2: something with spacess I dont care how many are there
You can have power and simplicity. Or you can choose more complex languages which require some of the oddest syntax, strangest function or chained method calls to do something as simple and elegant as was done above. Or you can chose to use languages marketed by a computer maker which look like little more than solutions looking for problems, and are square pegs to every round hole that exist or will be invented. PT Barnum's quote comes to mind, about fooling some of the people ...
I suspect the python could be made more intelligable. Ruby syntax isn't bad, and you might be able to do something similar to the Perl. But in all cases, imagine how much wasted time and effort one could go through by not using a language that is designed to make programmers more efficient.
The amount of wasted effort, re-inventing wheels is startling. The amount of criticism heaped upon tools that make the user of tools more productive, is mind boggling. Its even funnier when considering that the "write-only" aspects, i.e. the regular expressions of Perl, which give it quite a bit of awesome power, are being included now in a variety of languages. Imitation is the sincerest form of flattery.
If you want to use a powerful language that won't get in your way with brain-dead methods and weird syntactical gymnastics, look at Perl and related. If you dont mind languages that do get in your way, force you to do things in a particular manner, which is not conducive to solving your problems, prevents you from using powerful tools, look at some of the other languages.
Admin
Still no proper STL version given, so here's one:
void remove_spaces(std::string& s) { s.resize(std::remove(s.begin(), s.end(), ' ') - s.begin()); }
This is in-place, O(s.size())
Admin
void removeSpaces(char* c1) { if ( ! c1 || ! *c1 ) return; char c2 = c1;
}
Admin
Ack.
Much better:
void removeSpaces(char* c) { if ( ! c ) return;
}