代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void BubbleSort(char str[][20], int n)
{
int i, j, flag;
char temp[20];
for (i = n - 1; i >= 0; --i)
{
flag = 0;
for (j = 1; j <= i; ++j)
{
if (strcmp(str[j - 1], str[j]) == 1)
{
strcpy(temp, str[j - 1]);
strcpy(str[j - 1], str[j]);
strcpy(str[j], temp);
flag = 1;
}
}
if (flag == 0)
return;
}
}
int main()
{
char s[3][20];
int i;
for (i = 0; i < 3; ++i)
{
printf("Please enter one line of characters:");
scanf("%s", s[i]);
getchar();//吸收换行符
}
BubbleSort(s, 3);
for (i = 0; i < 3; ++i)
{
puts(s[i]);
}
return 0;
}
关注我获取更多编程方面的知识,和我共同进步吧~