本题要求编写程序,读入5个字符串,按由小到大的顺序输出。
输入格式:
输入为由空格分隔的5个非空字符串,每个字符串不包括空格、制表符、换行符等空白字符,长度小于80。
输出格式:
按照以下格式输出排序后的结果:
After sorted:
每行一个字符串
输入样例:
red yellow blue green white
输出样例:
After sorted:
blue
green
red
white
yellow
// Date:2020/3/24
// Author:xiezhg5
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 5
#define N 80
int main(void)
{
char a[M][N],b[N];
int i,j;
for(i=0;i<M;i++)
scanf("%s",&a[i]);
for(i=0;i<M;i++)
{
//j随i的变化而变化
for(j=0;j<M-i;j++)
{
if(strcmp(a[j],a[j+1])>0) //调用strcmp函数
{
//b代表b[]
strcpy(b,a[j]); //调用strcpy函数
strcpy(a[j],a[j+1]);
strcpy(a[j+1],b);
}
}
}
printf("After sorted:\n");
for(i=0;i<M;i++)
printf("%s\n",a[i]);
return 0;
}