Sorting a two string in C
The followings are my code
Code:
int i = 0; how can i make this to work? Thanks in advance. |
There's no primitive to do it in C as it's done by other languages. If there's a need to skip strcmp(), you should do it manually then.
|
i figured out the comparing part ....
i need to know how to sort the words now... i think my algorithm is correct (i already wrote this in Java) but it doesn't work. |
What immediately catches the eye:
Code:
if (result = 0) |
Re: Sorting a two string in C
Quote:
by the looks of the code word_array is an array of pointers to strings, is this correct? but then this line confuses me Code:
tmpArray = *word_array[i]; then word_array[i] is a pointer to a string, then dereferencing it gives the first char in the array, and you put this in an int(which is the same size as a char); it looks like you are trying to sort strings(whole words) but then you do Code:
*word_array[i] = *word_array[i+1]; |
Thanks for your reply.
i'll just post the function sort() Code:
int sort_words(char **word_array, int N ){ Thank you. |
as I said:
if(result = 0) will assign 0 to result and this will always evaluate to false and you will never do the swap There might, btw, be a couple of other things wrong (did not look at it). |
thanks Wim,
so i changed it to result ==0 but now it won't even go into the while loop... |
I modified my code a little
Code:
int sort(char **word_array, int N){ |
Quote:
Code:
tmpArray = *word_array[j]; why do you have two while loops? j is never reset so if the inner loop takes j to its max value the inner loop is exited and i is incremented but j is still its max value so the inner loop is not entered. why does the func return and int? |
value of tmpArray is the address of word_array[j] right?
word_array[0] = program word_array[1] = linux so word_array[0][0] = p word_array[0][1]= r word_array[1][0]= l word_array[1][2]= n returning int doesn't really matter. i just need to re-arrange (sort) the array and save it. When i run my program it stops after tmpArray = *word_array[j]; it won't run *word_array[j] = *word_array[j+1]; Why is that? Thanks. |
Quote:
|
then how can i get the result i want?
thanks. |
Code:
char* words[20]; |
yeah... but the problem is i need to make the array two dimensional because i first need to compare the two strings (each character) to see which one comes before.
so i can't do it like what you did. |
All times are GMT -5. The time now is 04:28 AM. |