• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

wchenfeng

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

编字符串排列函数sort,实现从键盘输入5个字符串(每个字符串长度小于30),其ascii码从小到大排列输出,冒泡法。

#include<stdio.h>
#include<string.h>
#define M 5
#define N 20

void swap(char a[], char b[]) {
	char st[N];
    strcpy(st, a);
    strcpy(a, b);
    strcpy(b, st);
}

void scanf_lists(char list[][N], int len)
{
	int i;

    for (i = 0; i < len; ++i) {
		scanf("%s", list[i]);
    }
}

void printf_lists(char list[][N], int len)
{
	int i;
    for (i = 0; i < len; ++i) {
        printf("%s\n", list[i]);
    }
}

void sort_soap(char list[][N], int len)
{
int i, j;
 for (i = 0;i <= len - 1;i++)
 {
  for (j = 0;j < len - 1 - i;j++)
  {
    if(strcmp(list[j],list[j+1])>0)
   
    swap(list[j],list[j+1]);
   
  }
 }
}

void main(void)
{
    char list[M][N];
    int i, j, p;

	scanf_lists(list, M);
    sort_soap(list, M);
    printf_lists(list, M);
}

 

posted on 2022-04-12 20:03  王陈锋  阅读(87)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3