30

冒泡法将字符串从小到大排序

Tag: C编程yixin @ 11:49

这个学期C语言课老师要求做了个作业:依次输入多个字符串,用冒泡法将字符串从小到大排序。当时还没学指针,就用了下面的方法,发出来大家参考参考,多多指教!

?Download yixin.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
int i,j;
char a[6][80],c[80];
printf("input 6 strings:\n");
for(i=0;i<6;i++) gets(a[i]);
printf("\n");
for(i=0;i<5;i++)
 for(j=i+1;j<6;j++)
  if(strcmp(a[i],a[j])>0)
 {
  strcpy(c,a[i]);
  strcpy(a[i],a[j]);
  strcpy(a[j],c);
 }
 printf("output from little to large:\n");
 for(i=0;i<6;i++) printf("%s\n",a[i]);
 getch();
 }

1 回复:“冒泡法将字符串从小到大排序”

  1. 企业信息采集

    博主博客做的不错,支持博主,以后会常来的!

发表回复