sort

V

void

Hi,
I hope you can help me to clarify this problem
http://rafb.net/paste/results/dgszQh47.html


I m trying to sort a trsing table in this way :
for a given strings
"toto"
''toto a"
''toto b"

the result should be
"toto" ''toto a" ''toto b"

In my implementation the result is
''toto a" ''toto b" "toto"
I really dont understand where i'm wrong (i'm using an old implementation of
standard libraries strncasecmp ... defined)
Anyone have an idea ?

BR.

--
struct
{
char m_file_path[MAX_PATH_LENGTH];
unsigned short m_name_index;
}s_entry_info;


m_file_path : represent the full path for an entry point (may be directory
or file)
m_name_index : The index from where the filename starts, i.e
m_file_path="/K/toto/titi.txt" m_name_index=7.
 
C

CBFalconer

void said:
.... snip ...

I m trying to sort a trsing table in this way :
for a given strings
"toto"
''toto a"
''toto b"

the result should be
"toto" ''toto a" ''toto b"

In my implementation the result is
''toto a" ''toto b" "toto"
I really dont understand where i'm wrong (i'm using an old
implementation of standard libraries strncasecmp ... defined)
Anyone have an idea ?

I have no idea what a trsing table is. However I do know that
there is no such thing as strncasecmp() function in standard C. In
fact it is not even legal to build such a function, since names
beginning with 'str' followed by a lower case letter are reserved
to the implementation.

It is possible you really want a function such as (untested):

#include <ctype.h>
int strIgcasecmp(const unsigned char *l, const unsigned char *r)
{
while (toupper(*l) == toupper(*r) && *l) {
r++; l++;
}
return (toupper(*l) < toupper(*r))
- (toupper(*l) > toupper(*r));
}

Maybe tolower would do better than toupper for some languages.
 
W

Walter Roberson

CBFalconer said:
I have no idea what a trsing table is. However I do know that
there is no such thing as strncasecmp() function in standard C. In
fact it is not even legal to build such a function, since names
beginning with 'str' followed by a lower case letter are reserved
to the implementation.

It might be a reserved name, but we don't know that it wasn't
provided as an implementation extension, so saying that it is not
even legal to build such a function is a little misleading.
Not legal as a user function, perhaps.

Looks like strncasecmp is in the XSH portion of The Single Unix Standard.
http://www.opengroup.org/pubs/online/7908799/xsh/strncasecmp.html
 
N

Nagaraj L

I hope you can help me to clarify this problemhttp://rafb.net/paste/results/dgszQh47.html
I m trying to sort a trsing table in this way :
for a given strings
"toto"
''toto a"
''toto b"

the result should be
"toto" ''toto a" ''toto b"

In my implementation the result is
''toto a" ''toto b" "toto"
I really dont understand where i'm wrong (i'm using an old implementation of
standard libraries strncasecmp ... defined)
Anyone have an idea ?
The function strncasecmp() is not a standard C function. It is
implementation dependent.
ASCII hex value for space is 0x20 and for new line it is 0x0A.
Inside the strncasecmp() function, implementation could be like
comparing which value is less. It may not be considering this condition
at all. You will have to modify strncasecmp() function or use your own
function if source is not available.

Regards,
Nagaraj L
 
P

pete

void said:
Hi,
I hope you can help me to clarify this problem
http://rafb.net/paste/results/dgszQh47.html

I m trying to sort a trsing table in this way :
for a given strings
"toto"
''toto a"
''toto b"

the result should be
"toto" ''toto a" ''toto b"

In my implementation the result is
''toto a" ''toto b" "toto"
I really dont understand where i'm wrong
(i'm using an old implementation of
standard libraries strncasecmp ... defined)
Anyone have an idea ?

/* BEGIN new.c output */

Before sorting:
toto b
toto
toto a

After sorting:
toto
toto a
toto b

/* END new.c output */



/* BEGIN new.c */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NMEMB(A) (sizeof (A) / sizeof *(A))

int comp(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}

int main (void)
{
unsigned index;
char *string[] = {"toto b", "toto", "toto a"};

puts("/* BEGIN new.c output */\n");
puts("Before sorting:");
for (index = 0; index != NMEMB(string); ++index) {
puts(string[index]);
}
qsort(string, NMEMB(string), sizeof *string, comp);
puts("\nAfter sorting:");
for (index = 0; index != NMEMB(string); ++index) {
puts(string[index]);
}
puts("\n/* END new.c output */");
return 0;
}

/* END new.c */
 
P

pete

pete wrote:
int comp(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}

I don't really see this as a case
where the n parameter in strncmp is useful.

Theses are my case insensitive variations:


#include <ctype.h>

int str_ccmp(const char *s1, const char *s2)
{
for (;;) {
if (*s1 != *s2) {
int c1 = toupper((unsigned char)*s1);
int c2 = toupper((unsigned char)*s2);

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

int str_cncmp(const char *s1, const char *s2, size_t n)
{
for (;;) {
if (n-- == 0) {
return 0;
}
if (*s1 != *s2) {
int c1 = toupper((unsigned char)*s1);
int c2 = toupper((unsigned char)*s2);

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top