PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > PLEASE - odd string sorting related problem
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
PLEASE - odd string sorting related problem

Réponse
 
LinkBack Outils de la discussion
Vieux 19/10/2007, 23h57   #1
cpptutor2000@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut PLEASE - odd string sorting related problem

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 .

  Réponse avec citation
Vieux 20/10/2007, 00h20   #2
Default User
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 .



Devise and algorithm for comparing such strings.

Implement the algorithm.




Brian
  Réponse avec citation
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
Vieux 20/10/2007, 00h44   #4
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: PLEASE - odd string sorting related problem

"cpptutor2000@yahoo.com" <cpptutor2000@yahoo.com> writes:
> 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.


What's obvious about that?

> I can convert all the non-digit characters to
> lowercase, but how do I deal with the non-alphanumeric characters?


I don't know. How do you want to deal with non-alphanumeric characters?

Using strcmp() directly is certainly a valid way to sort strings, but
you apparently want to map uppercase letters to lowercase before
comparing them. That still leaves a plethora of ways you might want
to compare strings that contain things other than letters. We have no
way of knowing (and C doesn't define) which of those ways is valid.

You need to decide how you want to do the comparisons. Once you've
done that, it's likely you'll be able to implement the comparison in C
yourself. If not, show us what you've tried and we can you fix
it.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 20/10/2007, 02h28   #5
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: PLEASE - odd string sorting related problem

>Could some C guru provide some hints on my problem? I am trying to

It might if you STATE A PROBLEM.

>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.


It is obvious to me that strcmp() would succeed in comparing any C
strings whatever, as long as you put no requirements on sort order
that conflict with what strcmp() does (and you didn't say anything
about sort order at all).

>I can convert all the non-digit characters to
>lowercase, but how do I deal with the non-alphanumeric characters?


It isn't necessary to convert punctuation to lower case in any
character set I'm aware of.

It is generally considered a hanging offense to deal characters
from the bottom of the deck.
  Réponse avec citation
Vieux 20/10/2007, 04h24   #6
J. J. Farrell
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: PLEASE - odd string sorting related problem

On Oct 19, 11:57 pm, "cpptutor2...@yahoo.com" <cpptutor2...@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.


What do you mean by "fail"? strcmp() will work perfectly well in these
cases.

> I can convert all the non-digit characters to
> lowercase, but how do I deal with the non-alphanumeric characters?


I've no idea - it depends how you want to deal with them. Why do you
want to do anything other than use their normal values?

> Any hints or suggestions would be greatly ful.


You need to define exactly what you want to do, then write code to do
it.

  Réponse avec citation
Vieux 20/10/2007, 21h01   #7
cpptutor2000@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: PLEASE - odd string sorting related problem

On Oct 19, 8:28 pm, gordonb.hw...@burditt.org (Gordon Burditt) wrote:
> >Could some C guru provide some hints on my problem? I am trying to

>
> It might if you STATE A PROBLEM.
>
> >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.

>
> It is obvious to me that strcmp() would succeed in comparing any C
> strings whatever, as long as you put no requirements on sort order
> that conflict with what strcmp() does (and you didn't say anything
> about sort order at all).
>
> >I can convert all the non-digit characters to
> >lowercase, but how do I deal with the non-alphanumeric characters?

>
> It isn't necessary to convert punctuation to lower case in any
> character set I'm aware of.
>
> It is generally considered a hanging offense to deal characters
> from the bottom of the deck.


As far as I remember from my trusty K & R C text,
the source code for the strcmp fumction is:

int strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)
(s2 - 1));
}

Given that, I am trying to find a way of sorting strings for example:
1. Bungie.net - TCP623
2. Doom(Id Sofware) - version 1
Obviously, the non-digit and non-alphanumeric (a -> z, A - > Z) cannot
be converted to lower case, how do I deal with these special
characters - straightforward application of 'strcmp' would not provide
very accurate results.


  Réponse avec citation
Vieux 20/10/2007, 21h25   #8
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: PLEASE - odd string sorting related problem

"cpptutor2000@yahoo.com" <cpptutor2000@yahoo.com> writes:
[...]
> As far as I remember from my trusty K & R C text,
> the source code for the strcmp fumction is:
>
> int strcmp(const char *s1, const char *s2)
> {
> while (*s1 == *s2++)
> if (*s1++ == 0)
> return (0);
> return (*(const unsigned char *)s1 - *(const unsigned char *)
> (s2 - 1));
> }


That's one possible definition for strcmp() (I'll assume it's correct,
but I haven't taken the time to check it). But strcmp() is specified
in terms of how it behaves. It needn't even be implemented in C.

> Given that, I am trying to find a way of sorting strings for example:
> 1. Bungie.net - TCP623
> 2. Doom(Id Sofware) - version 1
> Obviously, the non-digit and non-alphanumeric (a -> z, A - > Z) cannot
> be converted to lower case, how do I deal with these special
> characters - straightforward application of 'strcmp' would not provide
> very accurate results.


What. Are. You. Trying. To. Do. ???.

What do you mean by "accurate"?

There is no one definition of how to compare strings. You've told us
that you want to treat corresponding upper and lower case letters as
if they were equal ('A' and 'a' equal). You haven't given us a clue
about how you want to deal with non-alphanumeric characters.

For example, if you're comparing "foobar" vs. "FooBarBaz", you
apparently want to map all letters to lowercase, then use strcmp() to
compare "foobar" vs. "foobarbaz" (result: "foobar" < "FooBarBaz").

What if you're comparing "foo.bar" vs. "foo:bar"? What result do you
want? Do you want them to compare equal? Do you want "foo.bar" <
"foo:bar"? or "foo.bar" > "foo:bar"? Or do you not care as long as
you get a consistent ordering? Any of those would be equally correct.

We can't possibly guess how to accomplish your goal if you won't tell
us what your goal is.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h36.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15093 seconds with 16 queries