#include <iostream>
#include <string>
using namespace std;
int str_cmp(string str1, string str2)
{
int len1 = str1.length();
int len2 = str2.length();
for (int i = 0; i < len1 && i < len2; i++) if (str1[i] != str2[i]) return str1[i] - str2[i];
return len1 - len2;
}
int main()
{
int len = 0, i, j;
string str[100], temp;
cout << "输入国家名(以'#'结束):";
while (true)
{
cin >> temp;
if ('#' == temp[0]) break;
str[len++] = temp;
}
for (i = len - 1; i; i--)
for (j = 0; j < i; j++)
if (str_cmp(str[j], str[j + 1]) > 0)
{
temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
cout << "排序后输出:" << endl;
for (i = 0; i < len; i++)
cout << str[i] << ' '; cout << endl;
return 0;
}
结果如下: