-
strings, sorting
Hi, guys,
I've been working on sorting an array of strings... It is a work in progress, I did not take into consideration blank spaces, uppercase or lowercase characters as of yet. I need to get thi sfunction into a working condition to work on details.
When I compile it (no warnings or erros at all, gcc) and run the program, it finishes. So I take a look in my sorted array file and I see just blank lines.
Can anybody tell me what happened?
Code:
void InsertionSort(char *arr[])
{
char *helper = "......................";
int i, j;
for(i = 1; i < (int)strlen(arr[i])-1; i++) {
strcpy(helper, arr[i]);
j = i;
while(j >= 1 && (strcmp(helper, arr[j-1])<0)) {
strcpy(arr[j], arr[j-1]);
j -= 1;
}
strcpy(arr[j], helper);
}
}
-
I can give you a hint:
Code:
i < (int)strlen(arr[i])-1;
will not tell you how many strings there are in the array of strings.
You will have to pass that information to InsertionSort()
Kurt
-
Code:
char *helper = "......................";
strcpy(helper, arr[i]);
No no no no no no no.
char *helper gives you a pointer to a static string and you really shouldn't try to edit it at all. Use this instead:
Code:
char helper[] = "......................";
-
I tried to correct my code the way you suggested but still gave me the same empty file (empty meaning blank lines, exactly as amny as there should be). I am re-writing my code from scratch now, I'll post again if I have questions...
Thanks.
-