void sortstring(char *a3[],int len){
//冒泡排序
char *temp;//临时存放地址
for (int i = 0; i < len-1; i++){
for (int j = 0; j < len - 1 - i; j++){
//比较字符串大小用函数
if (strcmp(a3[j], a3[j + 1])>0)
{
//实现交换
temp = a3[j];
a3[j]=a3[j+1];
a3[j + 1] = temp;
}
}
}
}
int main(int argc, const char *argv[]){
//输入5个字符串并按字母顺序排列
char *a3[5] = {"wer","ad","fgg","rt","bg"};
for (int i = 0; i < 5; i++){
printf("%s\n", a3[i]);
}
printf("\n");
sortstring(a3,5);
for (int i = 0; i < 5; i++){
printf("%s\n",a3[i]);
}
return 0;
} |
|