Re: string comparison
Ben Pfaff wrote:[color=blue]
> Netocrat <netocrat@dodo. com.au> writes:
>
>[color=green]
>>Actually that's useless as a fix per your words - instead:
>> return *a == *b ? 0 : *a > *b ? 1 : -1;
>>
>>There are probably cheaper ways to do this knowing details about the
>>implementatio n but this is hopefully a strictly compatible general
>>version.[/color]
>
>
> Here's my favorite version. I got it from someone here on clc
> but don't remember whom:
> return *a < *b ? -1 : *a > *b;[/color]
It's ok, but I don't believe you would need the last comparison
if you check equaity(requiri ng a return of 0) within the loop.
while(*a++ == *b++)
if(*a == '\0') return 0;
return *a<*b?-1:1;
--
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapi dsys.com (remove the x to send email)
Ben Pfaff wrote:[color=blue]
> Netocrat <netocrat@dodo. com.au> writes:
>
>[color=green]
>>Actually that's useless as a fix per your words - instead:
>> return *a == *b ? 0 : *a > *b ? 1 : -1;
>>
>>There are probably cheaper ways to do this knowing details about the
>>implementatio n but this is hopefully a strictly compatible general
>>version.[/color]
>
>
> Here's my favorite version. I got it from someone here on clc
> but don't remember whom:
> return *a < *b ? -1 : *a > *b;[/color]
It's ok, but I don't believe you would need the last comparison
if you check equaity(requiri ng a return of 0) within the loop.
while(*a++ == *b++)
if(*a == '\0') return 0;
return *a<*b?-1:1;
--
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapi dsys.com (remove the x to send email)
Comment