l33t(idiot q=0; q eats shit; q++)
Okay, last year, when I was but a wee Freshmen in HighSchool, durring the late spring I was just about done with my Intro To Unix course, and I using Linux like a fanatic. I went up to my programming teacher (one guy taught Unix, C, C++, Data Structures, Assembly, and HTML) and asked him if I could skip Intro to Programming and C I and go right on ahead to C II. He said "Sure, knock yourself out". So I spent that summer learning to code C on my own. Well, come last fall, I coded nothing like the other students. At first it was just a format thing, but I got a program off of freshmeat to help me out. Anyway, I still look at problems differently then my teacher. For instance, we recently had an argument over white space removal from a string. He coded it this way, using two increments (in retrospect, I think I would use this way anyway):
while (str[i] != '\0') { if (str[j] == ' ') { ++j; continue; } str [i++] = str[j++] }
while (str[i] != '\0') { if (str[i] == ' ') { strcpy( str + i, str + (i + 1) ); continue; } i++; }
len = strlen(str); i = 0; while(str[0]) { while(str[i] == ' ') { i++; } str[0] = str[i]; str++; } str -= len - i;
String: 'The quick brown fox jumps over the lazy dog.' A: 1.49 Million B: .98 C: 1.42 D: 1.44 String: 'A B' String: ' ' String: 'AAAAA etc.' (~1024 long) A: 8.23 Million A: 10.39 Million A: 85.36 Thousand B: 7.77 B: 9.85 B: 107.69 C: 7.13 C: 8.65 C: 77.54 D: 8.38 D: 10.51 D: 78.55 String: 'i a m t h e v e r y etc.' String: 'AAAAA etc.' (~128 long) A: 628.15 Thousand A: 667.29 Thousand B: 138.16 B: 830.62 C: 625.33 C: 602.97 D: 655.06 D: 616.14 String: 'this is a string with normal words etc.' (~1024 long) A: 80.22 Thousand B: 5.13 C: 78.83 D: 78.34 String: 'a b c etc.' (~1024 long) A: 55.88 Thousand B: 1.02 C: 58.16 D: 58.92
Your code may perform faster if (and this is unlikely) large memory copies are optimized by your computer. In other words, it may be faster to copy a bunch of characters at a time than each character individually. However, this is pretty unlikely. There is one serious flaw in your code that you should be aware of. According to the C specification, calling strcpy() when the source and destination strings overlap in memory is undefined. This means that on certain compilers this could trash your string, write into bad memory, crash the process or any other of a variety of unpredictable things.
Your teacher's code will work correctly, personally, I try to avoid using continue. The Azure Monk's code example works as well, but uses pointer math, which I also find hard to read. The following is about as readable as I could make it:
int i = 0; int j = 0; while( str[ i ] != '\0' ) { while( str[ j ] == ' ' ) { j++; } str[ i++ ] = str[ j++ ]; } // note: at this point j - i will give the // number of whitespace characters removed
void strip(char * newstr, char * oldstr) { for (;*oldstr == ' ';oldstr++); *newstr = *oldstr; if (*oldstr) strip (newstr+1, oldstr+1); return; }
Let a little recursion into your life... Everybody needs a little bloat! To answer your node's question, though, NO, that does not make you look obfuscated. In fact, it's the more obvious of the two examples you present, as it's strings you're working with there and you keep it in those terms, although both are pretty simple. I'd recommend against strcpy for copying one char, though. Simple assignment would be faster...
$str =~ tr/\s//;
#include <stdio.h> main(t,_,a) char *a; { return!0<t?t<3?main(-79,-13,a+main(-87,1-_,main(-86,0,a+1)+a)): 1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13? main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t, "@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#\ ;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \ q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \ ){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \ iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \ ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \ }'+}##(!!/") :t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1) :0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1); }
The scary thing is, not only does it compile, but it actually, upon being run, outputs the full text of The Twelve Days of Christmas. No, I didn't write this. I stumbled on it almost by accident on some computer science student's public_html. And no, I don't have any clue how it works (well, I sort of do, but just looking at it for more than 10 seconds makes my head throb).
printable version chaos
Everything2 Help