/*字符串数组的冒泡排序,从小到大*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 10
void swap(char *a,char *b) //交换两个字符
{
char *temp;
temp = a;
a = b;
b = temp;
}
void bubble(char *str[],int m) //冒泡函数
{
int i = 0;
int j = 0;
for(i = 0;i < m-1;i++)
{
for(j = 0;j < m-i-1;j++)
{
if(strcmp(str[j+1],str[j]) > 0)
{
swap(str[j+1],str[j]);
}
}
}
}
int main()
{
char *str[N] = {0};
int m = 0; //输入行数
int i = 0;
for(i = 0;i < N;i++) //分配内存空间
{
str[i] = (char*)malloc(sizeof(char)*20);
}
printf("please input m行:\n");
scanf("%d",&m);
printf("please input string:\n");
for(i = 0;i <
C语言编程练习——字符串数组的冒泡排序,从小到大
最新推荐文章于 2024-04-09 13:36:32 发布