#include <stdio.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
char m[5][80];
char n[80];
int i,j;
//输入5个字符串,存储在m中,
for(i=0;i<5;i++){
scanf("%s",&m[i]);
}
//比较字符串的大小
for(i=1;i<5;i++){
for(j=0;j<5-i;j++){
if(strcmp(m[j], m[j+1])>0){
strcpy(n, m[j]);
strcpy(m[j], m[j+1]);
strcpy( m[j+1],n);
}
}
}
printf("After sorted:\n");
for(i=0;i<5;i++){
printf("%s\n",m[i]);
}
return 0;
}
习题8-7 字符串排序 (20 分)
最新推荐文章于 2022-01-20 16:17:13 发布
这是一个C语言程序,用于读取5个字符串并使用冒泡排序进行排序。程序首先获取用户输入的字符串,然后通过两层循环实现字符串的比较和交换,最后输出排序后的字符串列表。
2835

被折叠的 条评论
为什么被折叠?



