I want to split my array and sort after it using bubble sort and srttok, from 'b' to 'z' it's all right, but when it comes to 'a' it has some problem.
Here's my code:
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[]) {
char str[10000];
char *token[100] = { 0 };
const char s[5] = { ' ', ',', ';', ':', '.' };
gets(str);
int i = 0;
token[i] = strtok(str, s);
while (token[i] != NULL) {
i++;
token[i] = strtok(NULL, s);
}
for (int j = 0; j < i; j++) {
printf("%s\n", token[j]);
}
printf("i = %d\n", i);
char *temp;
int exchanged = 1;
for (int j = 0; exchanged && j < i - 1; j++) {
exchanged = 0;
for (int k = 0; k < i - 1 - j; k++) {
if (strcmp(token[k], token[k + 1]) > 0) {
temp = token[k];
token[k] = token[k + 1];
token[k + 1] = temp;
exchanged = 1;
}
}
}
printf("\nAfter sort\n");
for (int j = 0; j < 100; j++) {
printf("%s\n", token[j]);
}
printf("\n");
}
Here's my result
It works well when I type 'b' to 'z', and wrong when I type 'a', how did that happen?