#include <stdio.h>
#include <string.h>
//对字符串进行排序 "heima","itcast","TingHua","Peking","shuangYuan"
//注意 第二维不能省略
void sort(char arr[][20])
{
for (int i = 0; i< 5-1; i++) {
for (int j = 0; j< 5-i-1; j++) {
char temp[20] = {0};
//字符串比较函数
if (strcmp(arr[j], arr[j+1]) > 0) {
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j+1]);
strcpy(arr[j+1], temp);
}
}
}
}
int main(int argc, const char * argv[]) {
char arr[5][20]={ "heima","itcast","TingHua","Peking","shuangYuan"};
sort(arr);
// 打印检查
for (int i = 0 ; i<5; i++) {
printf("%s\n",arr[i]);
}
return 0;
} |
|