Hi,
I have a problem with sorting, concretely with bubblesort.
I have this sorting function:
...and I call this function with this:Code:void bubblesort(char arr[]) /* Function to sort an array */ { int i, j; /* Indexes */ char tmp; /* Temporary variable */ for (i = strlen(arr); i > 0; i--) for (j = 0; j < i; j++) if (arr[j] > arr[j + 1]) { /* Changing the value of neighbouring elements */ tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } }
For example, that in "str" there is string "spkjua".Code:bubblesort(str);
I would like to sort this "chars" alphabetically (so after that, in the "str" there will be "ajkpsu").
Everything is allright when I replace the 6-th line:
with for exampleCode:for (i = strlen(arr); i > 0; i--)
...thus I only changed "strlen(arr)" to "6" and it works. But with "strlen(arr)" it doesn't work, but I think IT IS THE SAME, because "strlen(arr)" will return me the value "6".Code:for (i = 6; i > 0; i--)
So does anybody know why the program doesn't work correctly with the "strlen(arr)" in the function?
P.S.: I didn't forget to write "#include <string.h>" at the beginning.
Thanks.
Petike.



LinkBack URL
About LinkBacks



