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

PLEASE HELP - 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 helpful. Thanks in advance
for your help.

Oct 19 '07 #1
4 2038
cp**********@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 helpful. Thanks in advance
for your help.

Devise and algorithm for comparing such strings.

Implement the algorithm.


Brian
Oct 19 '07 #2
cp**********@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 helpful. Thanks in advance
for your help.
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
Oct 19 '07 #3
"cp**********@yahoo.com" <cp**********@yahoo.comwrites:
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 help you fix
it.

--
Keith Thompson (The_Other_Keith) ks***@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"
Oct 19 '07 #4
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 helpful.
You need to define exactly what you want to do, then write code to do
it.

Oct 20 '07 #5

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

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
4
by: jarkkotv | last post by:
Hi everyone! I'm having a little problem when sorting the ArrayList and I was wondering if there is a .NET guru who can help me out :) I'm trying to sort ArrayList alphabetically in ASP.Net...
1
by: Lisa | last post by:
I have a web app that gets a recordset from the database and fills a grid. You can drilldown from this table to a detail table. Because the tables sometimes get huge, and because I have to go...
3
by: ntexchange05 | last post by:
I am trying to learn asp.net and build a site and i am using web matrix and vb.net. I have installed the MSDE on my windows xp pro, i am using the book calledbeginning dynamic websites with...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
4
by: _Raven | last post by:
Okay, I am playing with submitting forms with Ajax. I am trying to adapt this script to my forms: http://www.captain.at/howto-ajax-form-post-get.php I have included my code at the bottom of this...
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
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: 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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.