Hi,
I have a problem with sorting, concretely with bubblesort.
I have this sorting function:
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;
}
}
...and I call this function with this:
For example, that in "str" there is string "spkjua".
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:
Code:
for (i = strlen(arr); i > 0; i--)
with for example
Code:
for (i = 6; 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".
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.