输入n个单词,将它们(不区分大小写)顺序从小到大排列并输出:(类似英文字典排序)
输入样例
3 dog array Array
输出样例
array Array dog
int main() {
int x;//x个单词
scanf("%d", &x);
char arr[x][50];
char temp[50];
//指向首字母
char *p;
char *p0;
//判断大小写
int p_upper;
int p0_upper;
for (int i = 0; i < x; i++) {
scanf("%s", arr[i]);
}
for (int i = 0; i < x; i++) {
for (int j = x - 1; j > i; j--) {
//初始化
p_upper = 0;
p0_upper = 0;
p = &arr[j][0];//指向首字母
p0 = &arr[j - 1][0];
//大小写判断
if (*p >= 'A' && *p <= 'Z') {
p_upper = 1;
}
if (*p0 >= 'A' && *p0 <= 'Z') {
p0_upper = 1;
}
if (p_upper == p0_upper) {//同为大小写字母
if (*p0 > *p) {//冒泡
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j - 1]);
strcpy(arr[j - 1], temp);
}
} else if (p0_upper == 1) {
if (*p0 + 32 > *p) {
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j - 1]);
strcpy(arr[j - 1], temp);
}
} else {//p_upper==1 大写
if (*p0 > *p + 32) {//大写转小写比较
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j - 1]);
strcpy(arr[j - 1], temp);
}
}
}
}
//冒泡结束,开始打印
for (int i = 0; i < x; i++) {
printf("%s\n", arr[i]);
}
return 0;
}