473,397 Members | 2,077 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,397 software developers and data experts.

new strcomp() function.

Ever noticed when looking at a list of computer-sorted items that you get
entries like

aardvark
aardvark1
aardvark10
aardvark2
....

This is because the sorting program is calling strcmp() internally.

Here's a replacement that solves this problem.

int compstr(const char *s1, const char *s2)
{
long n1, n2;
char *end1, *end2;

while(*s1 && *s2)
{
if(isdigit(*s1))
{
if(isdigit(*s2))
{
n1 = strtol(s1, &end1, 10);
n2 = strtol(s2, &end2, 10);
if(n1 < n2)
return -1;
if(n1 > n2)
return 1;

s1 = end1;
s2 = end2;
}
else
return *s1 - *s2;
}
else if(*s1 == *s2)
{
s1++;
s2++;
}
else
return *s1 - *s2;
}

if(*s2)
return -1;
if(*s1)
return 1;

return 0;
}

A few issues remain. For instance, this function only works in ANSI or other
lexigraphically-ordered character set.

Also

aardvark1

and

aardvark001

will compare as equal.

Also it doesn't understnad decimal points. I don't know if this is an
advantage or not. In books you will commonly find figures labelled

fig1.1 ... fig1.9 fig1.10

on the other hand, 1.10 is lower than 1.9 in the decimal system.
Nov 14 '05 #1
1 6191

On Mon, 15 Dec 2003, Malcolm wrote:

Ever noticed when looking at a list of computer-sorted items that you get
entries like

aardvark
aardvark1
aardvark10
aardvark2
...

This is because the sorting program is calling strcmp() internally.

Here's a replacement that solves this problem.
<snip code>

You might be interested in this site:
http://sourcefrog.net/projects/natsort/
which seems to have [links to] a few C implementations of
"natural string comparison" functions, plus some Perl ones.
(Frankly, I was surprised this wasn't already built into
"standard" Perl somewhere -- but that's off-topic here.)

A few issues remain. For instance, this function only works in ANSI
or other lexigraphically-ordered character set.
Meaning that it sorts 'Anteater' before 'aardvark', and so on,
right?
Also
aardvark1
and
aardvark001
will compare as equal.


You could always solve this problem by replacing all your

return 0;
with
return strcmp(original_first_string, original_second_string);

I don't have any quibbles with the code itself -- looks pretty
good to me!

HTH,
-Arthur

Nov 14 '05 #2

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
by: steven scaife | last post by:
Ok i need to compare 2 values from my array but strComp no matter what returns 1 even though my comparison values are the same if i do this str1 = "wed" str2 = "wed" strComp(str1, str2)...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
32
by: Andrew Poulos | last post by:
I'm writing some ASP using js and I need to do a case sensitive SQL select. Googling gave me this: SELECT * FROM User WHERE Strcomp("Blue",,vbBinaryCompare)=0 Strcomp is from vbs. Is there a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.