在主函數中輸入10個等長的字符串。用另一函數對它們排序。然后在主函數輸出這10個已排好序的字符串
解題思路: 排序方式與數字比較沒什么不同,先遍歷比較找出最大的字符串,與第一個字符串進行交換,然后剩下的進行比較找出最大的字符串與第二個交換....
需要主機的就是字符串的比較采用strcmp接口,返回值大於0表示第一個字符串大於第二個字符串
答案:
#include<stdio.h>
#include<string.h>
void sort(char s[10][32])
{
int i, j;
for (i = 0; i < 10; i++){
for (j = i; j < 10; j++){
if (strcmp(s[i], s[j])> 0){
char tmp[32];
strcpy_s(tmp, 32, s[i]);
strcpy_s(s[i], 32, s[j]);
strcpy_s(s[j], 32, tmp);
}
}
}
}
int main()
{
char str[10][32];
printf("Please enter ten strings:\n");
for (int i = 0; i < 10; i++){
scanf_s("%s", str[i], 32);
}
sort(str);
printf("\n");
for (int i = 0; i < 10; i++){
printf("%s\n", str[i]);
}
system("pause");
return 0;
}