Path: utzoo!attcan!uunet!cs.utexas.edu!wuarchive!udel!princeton!drops.Princeton.EDU!pfalstad From: pfalstad@drops.Princeton.EDU (Paul John Falstad) Newsgroups: comp.lang.c Subject: Re: Comparing strings... Message-ID: <3343@idunno.Princeton.EDU> Date: 14 Oct 90 21:45:37 GMT References: <1990Oct13.190106.15615@ux1.cso.uiuc.edu> <3330@idunno.Princeton.EDU> <11486@alice.att.com> Sender: news@idunno.Princeton.EDU Organization: Princeton University, Princeton, New Jersey Lines: 34 In article <11486@alice.att.com> ark@alice.att.com (Andrew Koenig) writes: >In article <3330@idunno.Princeton.EDU>, pfalstad@phoenix.Princeton.EDU (Paul John Falstad) writes: >> int strcmp(char *s,char *t) >> { >> for (; *s && *s == *t; s++,t++); >> return *s-*t; >> } >Whether or not this works depends on your definition of the problem. >If you really want to follow the normal lexical convention that >the null string compares <= anything else, and you're on a machine >on which chars can be negative, then it doesn't work. OK. Declare the chars unsigned then. (I copied this with minor changes from K&R2, so I assumed it would be right.) Other optimizations could be made: int strcmp(char *s,char *t) /* declared register if you like */ { unsigned char c,d; while((c = (unsigned char) *s++) == (d == (unsigned char) *t++) && c); return c-d; } I've heard of faster ways that take advantage of parallelism by comparing a word or longword at a time. Can't remember if it was worth it or not. -- pfalstad@phoenix.princeton.edu The Germans are disputing it! Hegel is arguing that the reality is merely an a priori adjunct of non-absolutistic ethics; Kant, by the categorical imperative, is holding it ontologically exists only in the imagination; and Marx is claiming it was off sides.