Path: utzoo!utgpu!watmath!clyde!att!rutgers!mailrus!ames!haven!vrdxhq!daitc!jkrueger@daitc.daitc.mil From: jkrueger@daitc.daitc.mil (Jonathan Krueger) Newsgroups: comp.databases Subject: Re: Style (was: C-DATABASE B-PLUS a quick look) Summary: defensive programming: slow down and live Keywords: style correctness Message-ID: <262@daitc.daitc.mil> Date: 17 Dec 88 08:01:38 GMT References: <179@lakesys.UUCP> <2404@xyzzy.UUCP> Sender: jkrueger@daitc.daitc.mil Reply-To: jkrueger@daitc.daitc.mil (Jonathan Krueger) Followup-To: comp.databases Organization: Defense Applied Information Technology Center Lines: 45 In-reply-to: throopw@xyzzy.UUCP (Wayne A. Throop) In article <2404@xyzzy.UUCP>, throopw@xyzzy (Wayne A. Throop) writes: >Applying all of these, we get the "improved" fragment (using prototypes): > > #include > char *strupr( char *s ) { > register char *p; > for( p=s; *p != '\0'; p+=1 ) > *p = toupper( *p ); > return( s ); > } Try strupr("Foo Bar"); on ASCII machines it returns "&OO". The correct code is: #include char *strupr(s) char *s; { register char *p; for ( p=s; *p != '\0'; p+=1 ) *p = islower(*p) ? toupper(*p) : *p; return( s ); } As a matter of style, I prefer my version (shown to perform opposite operation): #include /* DLOWER: destructively lower-cases its arg, returns pointer to it */ char * dlower(str) char *str; { char *ptr = str; while (*ptr) { *ptr = isupper(*ptr) ? tolower(*ptr) : *ptr; ptr++; } return str; } References to reasons for my preferences: Plum-Hall Style Sheet, K&R. -- Jon -- Jonathan Krueger ...uunet!daitc!jkrueger jkrueger@daitc.arpa (703) 998-4777 My opinions are not necessarily those of my wallpaper.