输入5个国名,编程找出并输出按字典顺序排在最前面的国名。
提示:所谓字典顺序就是将字符串按由小到大的顺序排列,因此找出按字典顺序排在最前面的国名指的就是最小的字符串。
程序的运行结果示例:
Input five countries' names:
America↙
China↙
Japan↙
England↙
Sweden↙
The minimum is:America
输入格式: 国名输入用gets()函数。(字符串长度最大限制为30)
输出格式:
输入提示信息:"Input five countries' names:\n"
输出提示信息:"The minimum is:%s\n"
#include<stdio.h>
#include<String.h>
int main(){
char str[5][20],temp[30];
printf("Input five countries' names:\n");
for (int i = 0; i < 5; ++i) {
gets(str[i]);
}
for (int i = 0; i < 4; ++i) {
for (int j = 0; j <=3; ++j) {
int a=strcmp(str[j],str[j+1]);
if(a>0){
strcpy(temp,str[j]);
strcpy(str[j],str[j+1]);
strcpy(str[j+1],temp);
}
}
}
printf("The minimum is:%s\n",str[0]);
return 0;
}