c-style strings and sorting

Biennerienno

Weaksauce
Joined
May 2, 2001
Messages
71
Hey guys

I hate to do this, but i have been bashing my head against my laptop for the past few hours...

The program I must write, requires me to read in strings of data and store them as C-style char string. While reading the input, i have to sort the strings alphabetically.

The paper mentions two dimensions c-style arrays, but I cannot find anything on multidimensional char array.

So far I have deduced this:

Read in data -- easy enough

Check data after each char string
~to do this i have tried a few thigns but I keep ending up wiht a bunch of mush.
I can read in the data, and store it in the char array. and have tried comparing by tokenizing and a few other things. I do have moderate experience, so i guess all i am asking is for someone to point me in a direction, as I am completely lost now.

in the mean time, off to google groups...


thanks biennerienno
 
C-style strings are arrays and those are a bitch to move around, right?

You can sort arrays of integers easily tho, right? Why? Because they're all the same size & easy to swap.

Pointers are just like integers & easy to swap - try using them.

char *foo[]

aka

char **foo

aka

char foo[][]

just remember to malloc().
 
The runtime library provides qsort(). What's the problem?

.B ekiM
 
That would be awkward! I'd hope for const arguments. :)

.B ekiM
 
2 choices:

1. Make a linked list, read the strings in one at a time, and insertion sort the string read into the linked list (list of pointers so you don't have to actually move the string around, just leave it in the heap somewhere).

2. Read in all of the strings, implement a compare function, and use qsort().

Insertion sort is faster than qsort if you do it piece by piece I think
 
Fryguy8 said:
2 choices:

1. Make a linked list, read the strings in one at a time, and insertion sort the string read into the linked list (list of pointers so you don't have to actually move the string around, just leave it in the heap somewhere).

2. Read in all of the strings, implement a compare function, and use qsort().

Insertion sort is faster than qsort if you do it piece by piece I think

WTF would you suggest that somebody use qsort() in an intro programming class where they're probably learning to sort for the first time? That's like telling your little brother "Don't bother me with your multiplication tables, take my RPN calculator & do it that way".

Linked lists aren't much better, assuming that the OP doesn't completely get pointers (which I'm assuming is the case).


It sounds to me like the 'right' thing to do here (in the context of the class) is going to be a bubble-sort with an array of pointers to strings & making sure your comparison function actually derefernces those pointers.
 
ameoba said:
(in the context of the class)

In the context of what class? I didn't see the OP saying anything about being a student or taking a class.

.B ekiM
 
hey guys

Thanks for all the effort. I did the intelligent thing and went and talked to the instructor. I do have programming experience and have a decent grip on pointers, but really had never dealt with C-style strings before. It also came down to me getting one solution in my head and running it into the ground over and over again (long string and trying to use strtok).

when I went to my instructor, he wrote two things on the board, and after that I wanted to roll over and die because of the wasted time I had spent on this.

Code:
char x[500][50];
strcpy(x[i], stringX);

and explained how to address the two dimensional string array (500 rows, of 50 chars). Obv this wasnt the complete answer and I still had some playing to do, but I was glad I understood what he was saying as soon as I saw that on the board.

Jeesh, if I had just asked him to do this in the first place.

Anyways, thanks again guys...

someone also mentioned qsort(); I was looking at that function and couldnt find a good reference on it ( my C book was no help ). anyone want to explain it? (i do plan on learning sortin on my own, have done some already (selection sort) but i am still interested in learning)
 
ameoba said:
WTF would you suggest that somebody use qsort() in an intro programming class where they're probably learning to sort for the first time? That's like telling your little brother "Don't bother me with your multiplication tables, take my RPN calculator & do it that way".

Linked lists aren't much better, assuming that the OP doesn't completely get pointers (which I'm assuming is the case).


It sounds to me like the 'right' thing to do here (in the context of the class) is going to be a bubble-sort with an array of pointers to strings & making sure your comparison function actually derefernces those pointers.

Quoted from original message:
I can read in the data, and store it in the char array. and have tried comparing by tokenizing and a few other things. I do have moderate experience, so i guess all i am asking is for someone to point me in a direction, as I am completely lost now.

With only that as background to go on, I have no basis or reasoning to suggest a subpar solution. Documentation is readily available for qsort. It's not exactly brain science to learn generics..
 
Thats a space inefficient / simple way to do it, but by the sound of it you havent been taught dynamic mem allocation in c yet. So that will work and keep things simple.

As for qsort, it has kinda nasty syntax (requires you to void * all your pointers so it will work with any data type etc) Just sort it yourself, if you want to figure it out just google "unix man pages [function]" and a ton of online manual pages should come up detailing whatever function you want.
 
Back
Top