I have an array of strings that I need to sort alphabetically from a to z. What is the best way to do this? Could u please include a code sample for me to look at?
I have an array of strings that I need to sort alphabetically from a to z. What is the best way to do this? Could u please include a code sample for me to look at?
You can store all the letters into a character array and then use soemthing like bubblesort to sort them out using their ascii values, it's quite simple.
That is what i want to do. The trouble is... I've had one class in C and that was over a year ago and I don't remember how to do that. I have the the words in the array. The array is called word[50][20]. I just need to know how to sort them. Thanks.
Why have a double-subscripted array? Just do this:
nice and easy, if you still don't understand i can give you teh full code when i have time.Code:char array[ 26 ] = { 'a', 'b', 'c' ........ }; bubblesort( array ) { /* some other code */ for ( i = 0; i < 26; i++ ) for ( j = 0; j < 25; j++ ) if ( array[ i ] > array[ i + 1 ] ) swap them
Because when I read the array in, I have to limit the words to 20 characters or less. That is why I have the the second value.
why don't you use qsort and strcmp, which should be given to you in the stdlib. Try this example, and i'm sure you'll be able to figure out how to use it. Read your compiler documentation for more info on qsort.
easy as that.Code:#include <stdlib.h> #include <stdio.h> #include <string.h> int main () { char names[4][10] = {"george", "jim", "mary", "bob"}; int elements = 4, i; qsort (names, elements, 10, ( int (*) (const void*, const void*)) strcmp); for (i = 0; i < elements; ++i) printf ("%s\n", names[i]); return 0; }
Ok here is what I'm trying to do:
for(i=0;i<50;i++) //Sorts the contents of the array
{
// for(j=0;j<20;j++)
//{
if(strcmp(word[i+1],word[i])>0)
{
strcpy(datahold,word[i]);
strcpy(word[i],word[i+1]);
strcpy(word[i+1],datahold);
}
//}
}
Is there a way to make this work? This is the way I need to do it for the class. Plz help!!
first thing I would do is sit down with your professor because you don't really understand what you're trying to do. you do want a firm grasp on the material, right?
If you can't you qsort, here's a easy bubble sort that will work for you.
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> void b_sort_strings ( char * array[], int size) { char* temp; int i, j; for ( i = 0; i < size; ++i) for ( j = 0; j < size; ++j) if ( strcmp (array [i], array[j] < 0) { temp = array[i]; array[i] = array[j]; array[j] = temp; } }
I mean, why limit to 20 for the array? And if you're just comparing characters you don't need strcmp().
>And if you're just comparing characters you don't need strcmp().
I agree, comparing strings with a function call is wasteful if you only need to sort by the first character in that string. In fact, even sorting the strings is wasteful since you can create a second array of size 20 containing pointers to the first element of each string.
Sort the array of pointers by dereferencing them and comparing the characters pointed to. So instead of sorting arrays of 20 chars and being very clumsy about it, you sort little pointers. Then to read the sorted array, loop through the pointer array and dereference each string. They'll come out in sorted order.
Piece of cake.
-Prelude
My best code is written with the delete key.
To say what Prelude was saying in another way, you swap the addresses of your data, not the actuall data.
WTF are you guys talking about? what if you have two strings "steve" and "stephen". Then what do you do?. you have to account for duplicate characters...
The unreigistered guy is right. That is what I have to do. I have to put 50 words in alphabetical order. So I have to account for duplicate characters in the words. That is why I have the 2nd dimension in the array.
http://www.google.com/search?hl=en&q=bubble+sort+C
Gee, only 126,000 results, and that's just for bubble sort. Bubble sort's crap anyways - I've had professors who would fail me if they ever saw that in a program.
-Govtcheez
[email protected]