Afficher un message
Vieux 20/10/2007, 00h39   #3
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: PLEASE - odd string sorting related problem

cpptutor2000@yahoo.com wrote:
>
> Could some C guru provide some hints on my problem? I am trying to
> sort an array of character strings, where each string contains
> lowercase, uppercase, digits as well as non-alphanumeric characters as
> '-', '(' or '/'. Obviously, standard C functions as 'strcmp' would
> fail in these cases. I can convert all the non-digit characters to
> lowercase, but how do I deal with the non-alphanumeric characters?
> Any hints or suggestions would be greatly ful. Thanks in advance
> for your .


You could use a case insensitive variation on strcmp:

#include <ctype.h>
int str_ccmp(const char *s1, const char *s2)
{
for (; {
if (*s1 != *s2) {
const int c1 = tolower((unsigned char)*s1);
const int c2 = tolower((unsigned char)*s2);

if (c2 != c1) {
return c2 > c1 ? -1 : 1;
}
} else {
if (*s1 == '\0') {
return 0;
}
}
++s1;
++s2;
}
}

--
pete
  Réponse avec citation
 
Page generated in 0,05081 seconds with 9 queries