473,386 Members | 1,815 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,386 software developers and data experts.

Sorting a string aray in order of string length

Given an array of strings (i.e char *strings[10]), write C code to sort the
list in increasing order of string length. Run time efficiency is a primary
concern.

Thanks a lot!
Nov 13 '05 #1
9 14337
C.K. wrote:
Given an array of strings (i.e char *strings[10]), write C code to sort the
list in increasing order of string length. Run time efficiency is a primary
concern.

Thanks a lot!


Do your own homework?

Tom

Nov 13 '05 #2
No, I am now enjoying my summer vacation.

This is a question that I find from 'C Challenge' of a website. I am just
curious on how to solve it in an elegant way.

Thanks in advance!
"Tom St Denis" <to********@iahu.ca> ?????
news:_y********************@news01.bloor.is.net.ca ble.rogers.com...
C.K. wrote:
Given an array of strings (i.e char *strings[10]), write C code to sort the list in increasing order of string length. Run time efficiency is a primary concern.

Thanks a lot!


Do your own homework?

Tom

Nov 13 '05 #3
C.K. wrote:

Given an array of strings (i.e char *strings[10]),
write C code to sort the list in increasing order of string length.
Run time efficiency is a primary concern.


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

int compare(const void *arg1, const void *arg2)
{
size_t L1, L2;

L1 = strlen(arg1);
L2 = strlen(arg2);
return L2 > L1 ? -1 : L1 > L2;
}

qsort
(strings, sizeof strings/sizeof *strings, sizeof *strings, compare);
Nov 13 '05 #4
"C.K." wrote:

Given an array of strings (i.e char *strings[10]), write C code to sort the
list in increasing order of string length. Run time efficiency is a primary
concern.


Why is run time efficiency a primary concern when sorting
an array of just ten elements? With such a short array, how
do you even hope to measure the run time to find out what kind
of efficiency you've achieved?

--
Er*********@sun.com
Nov 13 '05 #5
C.K. wrote:
No, I am now enjoying my summer vacation.

This is a question that I find from 'C Challenge' of a website. I am just
curious on how to solve it in an elegant way.


The best way is to *try it yourself*, *then* ask for suggestions.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #6


"C.K." wrote:

No, I am now enjoying my summer vacation.

This is a question that I find from 'C Challenge' of a website. I am just
curious on how to solve it in an elegant way.

Don't top-post. Your replies belong following properly trimmed quotes.

The point of a C challenge is for YOU to work on it. Do your best and
post it here for critique. We don't do programming for random people to
admire.


Brian Rodenborn
Nov 13 '05 #7
pete <pf*****@mindspring.com> wrote:
C.K. wrote:

Given an array of strings (i.e char *strings[10]),
write C code to sort the list in increasing order of string length.
Run time efficiency is a primary concern.


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

int compare(const void *arg1, const void *arg2)
{
size_t L1, L2;

L1 = strlen(arg1);
L2 = strlen(arg2);
return L2 > L1 ? -1 : L1 > L2;
}

qsort
(strings, sizeof strings/sizeof *strings, sizeof *strings, compare);


You obviously didn't test this, because that won't work at all. The
const void * arguments qsort passes to the compare function are pointers
to the array elements - not the array elements themselves. In this
case, they're pointer-to-pointer-to-char, converted to void *. In this
case, the correct comparison function would be:

int lencomp(const void *a, const void *b)
{
char * const *sa = a;
char * const *sb = b;
size_t alen = strlen(*sa);
size_t blen = strlen(*sb);

return (alen > blen) - (alen < blen);
}

- Kevin.

Nov 13 '05 #8
Kevin Easton wrote:

pete <pf*****@mindspring.com> wrote:
C.K. wrote:

Given an array of strings (i.e char *strings[10]),
write C code to sort the list in increasing order of string length.
Run time efficiency is a primary concern.


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

int compare(const void *arg1, const void *arg2)
{
size_t L1, L2;

L1 = strlen(arg1);
L2 = strlen(arg2);
return L2 > L1 ? -1 : L1 > L2;
}

qsort
(strings, sizeof strings/sizeof *strings, sizeof *strings, compare);


You obviously didn't test this, because that won't work at all.
The const void * arguments qsort passes to the compare function
are pointers to the array elements
- not the array elements themselves.
In this case, they're pointer-to-pointer-to-char,
converted to void *.
In this case, the correct comparison function would be:

int lencomp(const void *a, const void *b)
{
char * const *sa = a;
char * const *sb = b;
size_t alen = strlen(*sa);
size_t blen = strlen(*sb);

return (alen > blen) - (alen < blen);
}


Thank you.
I make a mistake about that bad,
almost every single time that I post untested code.

My compiler likes it better this way:

char const **sa = a;
char const **sb = b;

--
pete
Nov 13 '05 #9
pete <pf*****@mindspring.com> wrote:
Kevin Easton wrote: [...]
int lencomp(const void *a, const void *b)
{
char * const *sa = a;
char * const *sb = b;

[...] My compiler likes it better this way:

char const **sa = a;
char const **sb = b;


Then your compiler is broken :). const void *a; and char * const *sa;
are equivalently-qualified pointers, but char const **sa; is not. The
first points to a const-qualified void, the second to a const-qualified
pointer to char, but the third points to a non-qualified pointer to a
pointer to const-qualified char.

- Kevin.

Nov 13 '05 #10

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

Similar topics

13
by: usgog | last post by:
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two...
18
by: Metro12 | last post by:
In the <basic_string.h>, I find the implementation of these two functions. But I can't understand the difference between them. Please give me some help! //basic_string::c_str() const _CharT*...
4
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
4
by: Jason Gleason | last post by:
What's the most efficient way to get the number of occurences of a certain string in another string..for instance i'm using the following code right now... private int CharacterCounter(String...
6
by: Webgour | last post by:
How to go from string "abc" to string "a|b|c"?
4
by: craigkenisston | last post by:
Hi, If I initialize a string like this : string s = new string(' ', 20); s = "Hi!"; Console.WriteLine( "" ); It clearly shows that the declaration of the initial length of the string...
4
by: Emilio | last post by:
Question about Shared Sub Connect(server As , message As ) Why is in square brackets? Is it like Shared Sub Connect(server() As String, message() As String)
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
6
by: Charlie | last post by:
Hi: Is there any difference between string.Empty and String.Empty? And what is the benefit of using it over "". Thanks, Charlie
6
by: John Dinning | last post by:
In 'C' it was possible to format a string to a fixed length, e.g. to 10 characters: printf("%10.10s","123456789012345"); would return "1234567890", and printf("%10.10s","12345"); would...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.