473,383 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

sort

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.

Nov 6 '06 #1
5 1386
void wrote:
>
.... 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 7 '06 #2
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>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...rncasecmp.html
--
Prototypes are supertypes of their clones. -- maplesoft
Nov 7 '06 #3
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

Nov 7 '06 #4
void wrote:
>
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 */
--
pete
Nov 7 '06 #5
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;
}
}

--
pete
Nov 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Philips | last post by:
I recently had the need to sort a large number of lists of lists, and wondered if an improvement to the Decorate-Sort-Undecorate idiom is in the works. Ideally, I would like to sort the list of...
1
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None,...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
7
by: Stuart | last post by:
The stl::sort() that comes with Dev Studio 6 is broken (it hits the degenerate case in a common situation). I have a replacement. I would like to globally do "using namespace std; except use my...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
10
by: Woody Ling | last post by:
In 32 bits DB2 environment, is it meaningful to set sheapthres larger than 256MB for the following case.. 1. Intra-parallel is ON 2. Intra-parallel is OFF
5
by: neehakale | last post by:
I know that heap sort,quick sort and merg sort are the faster sorting algoritms than the bubble sort,selection sort,shell sort and selection sort. I got an idea,in which situation we can use...
5
by: neocortex | last post by:
Hello! I am a newbie in Python. Recently, I get stuck with the problem of sorting by two criteria. In brief, I have a two-dimensional list (for a table or a matrix). Now, I need to sort by two...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.