Comparing strings...

Tim Pierce twpierce at amherst.bitnet
Tue Oct 16 20:17:11 AEST 1990


In article <1990Oct13.190106.15615 at ux1.cso.uiuc.edu>,
gordon at osiris.cso.uiuc.edu (John Gordon) writes:

> 	Hmmm... Well, here's what I came up with:
> 
> int compare_strings(char s1[], char s2[])
> {
>   int i;
> 
>   for(i = 0; s1[i] && s2[i]; i++)
>   {
>     if(s1[i] > s2[i])
>       return(S1_BIGGER);
>     else if(s1[i] < s2[i])
>       return(S2_BIGGER)
>   }

Correct me if I'm wrong here (being a C neophyte myself), but shouldn't you be
using a while loop rather than a for loop? It looks to me as if this code will
return only the relative sizes of the last characters in the string, not
stopping once an inequality is hit. Or does the return() statement imply a
break?

----------------------------------------------------------------------------
Tim Pierce		| "They call television a medium. That is because
twpierce at amherst.bitnet	|  it is neither rare nor well done." - Ernie Kovacs
----------------------------------------------------------------------------



More information about the Comp.lang.c mailing list