- 首页 > it技术 > >
输入5个字符串,排序后输出!
#include "stdio.h"
#include
#include
#define MAX 10
void main()
{ int i,j;
char str[5][MAX] = {0};
//char s[5];
printf("请输入5个字符串!\n");
for(i = 0;
i<5;
i++)
{
scanf("%s",str[i]);
}
printf("\n");
for(j = 0;
j<4;
j++)
{
for (i = 0;
i< 4-j;
i++)
{
if(strcmp(str[i],str[i+1])> 0)
{
char temp[MAX] = {0};
//还得初始化
strcpy(temp,str[i+1]);
strcpy(str[i+1],str[i]);
strcpy(str[i],temp);
//temp = str[i];
//str[i] = str[i+1];
//str[i+1] = temp;
}
}
}
for(i = 0;
i<5;
i++)
{
printf("%s\n",str[i]);
}
}
推荐阅读