鱼C论坛

 找回密码
 立即注册
查看: 1231|回复: 2

[已解决]字符串按大小排序

[复制链接]
发表于 2021-1-12 23:48:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5.     char a[10],b[10],c[10];
  6.     gets(a);
  7.     gets(b);
  8.     gets(c);
  9.     int i,j,a1,b1,c1;
  10.     a1=strlen(a);
  11.     b1=strlen(b);
  12.     c1=strlen(c);
  13.     char temp;
  14.     for(i=0;i<a1-1;i++)
  15.     {
  16.         for(j=0;j<a1-1-i;j++)
  17.         {
  18.             if(a[j]>a[j+1])
  19.             {
  20.                 temp=a[j];
  21.                 a[j]=a[j+1];
  22.                 a[j+1]=temp;
  23.             }
  24.         }
  25.     }
  26.     printf("%s",a);
  27.     printf("\n");
  28.     for(i=0;i<b1-1;i++)
  29.     {
  30.         for(j=0;j<b1-1-i;j++)
  31.         {
  32.             if(b[j]>b[j+1])
  33.             {
  34.                 temp=b[j];
  35.                 b[j]=b[j+1];
  36.                 b[j+1]=temp;
  37.             }
  38.         }
  39.     }
  40.     printf("%s",b);
  41.     printf("\n");
  42.     for(i=0;i<c1-1;i++)
  43.     {
  44.         for(j=0;j<c1-1-i;j++)
  45.         {
  46.             if(c[j]>c[j+1])
  47.             {
  48.                 temp=c[j];
  49.                 c[j]=c[j+1];
  50.                 c[j+1]=temp;
  51.             }
  52.         }
  53.     }
  54.     printf("%s",c);
  55.     return 0;
  56. }
复制代码

求助,找半天了
最佳答案
2021-1-13 01:35:58
这个题目,以前也回答过,当时的题目要求用指针写两个函数,现不作修改作为每天十个回帖之一,供你参考:
  1. // 字符串从小到大输出
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main()
  5. {
  6.         void sort(char *name[], int n);    //函数声明
  7.         void print(char *name[], int n);    //函数声明
  8.         char *name[] = { "I love C programing design","Yangzi river is our matherland river","Guangdong unveils development plan for the Greater Bay Area","China is a great country","Any difficulty can hard to beat heroic Chinese people!" };    //定义指针数组,它的元素分别指向5个字符串
  9.         int n = 5;
  10.         sort(name, n);    //调用sort函数,对字符串排序
  11.         print(name, n);    //调用print函数,输出字符串
  12.         return 0;
  13. }

  14. void sort(char *name[], int n)    //定义sort函数
  15. {
  16.         char *temp;
  17.         int i, j, k;
  18.         for (i = 0;i < n - 1;i++)    //用选择法排序
  19.         {
  20.                 k = i;
  21.                 for (j = i + 1;j < n;j++)
  22.                 {
  23.                         if (strcmp(name[k], name[j]) > 0)
  24.                         {
  25.                                 k = j;
  26.                         }
  27.                         if (k != i)
  28.                         {
  29.                                 temp = name[i];
  30.                                 name[i] = name[k];
  31.                                 name[k] = temp;
  32.                         }
  33.                 }
  34.         }
  35. }


  36. void print(char *name[], int n)    //定义print函数
  37. {
  38.         int i;
  39.         for (i = 0;i < n;i++)
  40.         {
  41.                 printf("%s\n", name[i]);    //按指针数组元素的顺序输出它们所指向的字符串
  42.         }
  43.         printf("\n");
  44. }
复制代码
1.png
2.png
3.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-13 00:36:22 | 显示全部楼层
本帖最后由 jackz007 于 2021-1-13 00:38 编辑
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(void)
  4. {
  5.         char a[10] , b[10] , c[10] , * p[4]             ;
  6.         int i , j                                       ;
  7.         gets(a)                                         ;
  8.         gets(b)                                         ;
  9.         gets(c)                                         ;
  10.         p[0] = a                                        ;
  11.         p[1] = b                                        ;
  12.         p[2] = c                                        ;
  13.         for(i = 0 ; i < 2 ; i ++) {
  14.                 for(j = i + 1 ; j < 3 ; j ++) {
  15.                         if(strcmp(p[i] , p[j]) > 0) {
  16.                                 p[3] = p[i]             ;
  17.                                 p[i] = p[j]             ;
  18.                                 p[j] = p[3]             ;
  19.                         }
  20.                 }
  21.         }
  22.         for(i = 0 ; i < 3 ; i ++) printf("%s\n" , p[i]) ;
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-13 01:35:58 | 显示全部楼层    本楼为最佳答案   
这个题目,以前也回答过,当时的题目要求用指针写两个函数,现不作修改作为每天十个回帖之一,供你参考:
  1. // 字符串从小到大输出
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main()
  5. {
  6.         void sort(char *name[], int n);    //函数声明
  7.         void print(char *name[], int n);    //函数声明
  8.         char *name[] = { "I love C programing design","Yangzi river is our matherland river","Guangdong unveils development plan for the Greater Bay Area","China is a great country","Any difficulty can hard to beat heroic Chinese people!" };    //定义指针数组,它的元素分别指向5个字符串
  9.         int n = 5;
  10.         sort(name, n);    //调用sort函数,对字符串排序
  11.         print(name, n);    //调用print函数,输出字符串
  12.         return 0;
  13. }

  14. void sort(char *name[], int n)    //定义sort函数
  15. {
  16.         char *temp;
  17.         int i, j, k;
  18.         for (i = 0;i < n - 1;i++)    //用选择法排序
  19.         {
  20.                 k = i;
  21.                 for (j = i + 1;j < n;j++)
  22.                 {
  23.                         if (strcmp(name[k], name[j]) > 0)
  24.                         {
  25.                                 k = j;
  26.                         }
  27.                         if (k != i)
  28.                         {
  29.                                 temp = name[i];
  30.                                 name[i] = name[k];
  31.                                 name[k] = temp;
  32.                         }
  33.                 }
  34.         }
  35. }


  36. void print(char *name[], int n)    //定义print函数
  37. {
  38.         int i;
  39.         for (i = 0;i < n;i++)
  40.         {
  41.                 printf("%s\n", name[i]);    //按指针数组元素的顺序输出它们所指向的字符串
  42.         }
  43.         printf("\n");
  44. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 13:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表