鱼C论坛

 找回密码
 立即注册
查看: 991|回复: 5

[已解决]求指正-C语言-字符串&&排序

[复制链接]
发表于 2020-12-12 19:31:52 | 显示全部楼层 |阅读模式

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

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

x
  1. /*编写程序,其功能是:从键盘输入一个字符串,对字符串下标为奇数的字符按ASCII码大小递增排序,下标为偶数不变*/
  2. #include<stdio.h>
  3. #include<string.h>
  4. #define M 50
  5. void main()
  6. {
  7.         char str[M];
  8.         char temp;
  9.         int i,j;
  10.         scanf("%s",str);
  11.         temp=str[1];
  12.         /*printf("%d",strlen(str));*/
  13.         for(i=1;i=strlen(str)-1;i+=2)
  14.         {
  15.                 for(j=1;j=strlen(str)-i-1;j+=2){
  16.                         temp=str[j];
  17.                         str[j]=str[i];
  18.                         str[i]=temp;
  19.                 }
  20.         }
  21. }
复制代码
最佳答案
2020-12-12 19:55:12
本帖最后由 jackz007 于 2020-12-12 19:58 编辑

        排序的要点是要对有先后次序的两个数做比较,合乎要求的维持原样,否则就交换位置。但是,在楼主的代码中,却没有看到有这样的比较。
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define M 50

  4. int main(void)
  5. {
  6.         char str[M] , temp                      ;
  7.         int i , j , m                           ;
  8.         scanf("%s" , str)                       ;
  9.         for(m = strlen(str) , i = 1 ; i < m - 2 ; i += 2) {
  10.                 for(j = i + 2 ; j < m ; j += 2) {
  11.                         if(str[j] < str[i]) {
  12.                                 temp = str[i]   ;
  13.                                 str[i] = str[j] ;
  14.                                 str[j] = temp   ;
  15.                         }
  16.                 }
  17.         }
  18.         printf("%s\n", str)                     ;
  19. }
复制代码

        编译、运行实况
  1. D:\00.Excise\C>cl x.c
  2. Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804
  3. Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

  4. x.c
  5. Microsoft (R) Incremental Linker Version 6.00.8447
  6. Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

  7. /out:x.exe
  8. x.obj

  9. D:\00.Excise\C>x
  10. a9b8c7d6e5f4g3h2i1j0
  11. a0b1c2d3e4f5g6h7i8j9

  12. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-12 19:55:12 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2020-12-12 19:58 编辑

        排序的要点是要对有先后次序的两个数做比较,合乎要求的维持原样,否则就交换位置。但是,在楼主的代码中,却没有看到有这样的比较。
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define M 50

  4. int main(void)
  5. {
  6.         char str[M] , temp                      ;
  7.         int i , j , m                           ;
  8.         scanf("%s" , str)                       ;
  9.         for(m = strlen(str) , i = 1 ; i < m - 2 ; i += 2) {
  10.                 for(j = i + 2 ; j < m ; j += 2) {
  11.                         if(str[j] < str[i]) {
  12.                                 temp = str[i]   ;
  13.                                 str[i] = str[j] ;
  14.                                 str[j] = temp   ;
  15.                         }
  16.                 }
  17.         }
  18.         printf("%s\n", str)                     ;
  19. }
复制代码

        编译、运行实况
  1. D:\00.Excise\C>cl x.c
  2. Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804
  3. Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

  4. x.c
  5. Microsoft (R) Incremental Linker Version 6.00.8447
  6. Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

  7. /out:x.exe
  8. x.obj

  9. D:\00.Excise\C>x
  10. a9b8c7d6e5f4g3h2i1j0
  11. a0b1c2d3e4f5g6h7i8j9

  12. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-12 20:09:07 | 显示全部楼层
本帖最后由 风过无痕1989 于 2020-12-12 23:47 编辑

下面的程序已经调试好了,并提供两种输出方式(因题目没有给出输出要求),用开关 #if() . . . #endif 控制,哪个为 #if(1) 就使用哪种方式输出:
  1. /*编写程序,其功能是:从键盘输入一个字符串,对字符串下标为奇数的字符按ASCII码大小递增排序,下标为偶数不变*/
  2. #include<stdio.h>
  3. #include<string.h>
  4. #define M 50
  5. void main()
  6. {
  7.         char str[M], sj[M];
  8.         char temp;
  9.         int i, j, n, count = 0;
  10.         scanf("%s", str);
  11.         /*printf("%d",strlen(str));*/
  12.         n = strlen(str);
  13.         for (i = 0;i < n;i++)  // 将下标为奇数的元素转入另一个数组
  14.         {
  15.                 if (i % 2 != 0)
  16.                 {
  17.                         sj[count] = str[i];
  18.                         str[i] = '9';    // 作个标志,该单元字符已被移出
  19.                         count++;
  20.                 }
  21.         }
  22.         for (i = 0;i < count;i++)     // 冒泡排序
  23.         {
  24.                 for (j = 0;j < count - 1;j++)
  25.                 {
  26.                         if (sj[j] > sj[j + 1])

  27.                         {
  28.                                 temp = sj[j];
  29.                                 sj[j] = sj[j + 1];
  30.                                 sj[j + 1] = temp;
  31.                         }
  32.                 }
  33.         }
  34.         sj[count] = '\0';     // 在字符串的末尾加上一个结束符
  35.         // 排序后插回原数组输出
  36. #if(0)
  37.         j = 0;
  38.         for (i = 0;i < n;i++)  // 将下标为奇数的元素转入原数组
  39.         {
  40.                 if (i % 2 != 0)
  41.                 {
  42.                         str[i] = sj[j];
  43.                         j++;
  44.                 }
  45.         }
  46.         printf("%s\n", str);
  47.         printf("\n");
  48. #endif
  49.         // 排序后分别输出
  50. #if(1)
  51.         for (i = 0;i < n;i++)  // 后面不为字符9的元素前移
  52.         {
  53.                 for (j = 0;j < n;j++)
  54.                 {
  55.                         if (str[i] == '9' && str[j] != '9' && j > i && str[j] != '\n')
  56.                         {
  57.                                 str[i] = str[j];
  58.                                 str[j] = '9';
  59.                         }
  60.                 }
  61.         }
  62.         for (i = 0;i < n;i++)
  63.                 if (str[i] == '9')
  64.                         str[i] = '\0';    // 在字符串的末尾加上一个结束符
  65.         printf("%s\n", str);
  66.         printf("%s\n", sj);
  67. #endif
  68. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-12 23:48:18 | 显示全部楼层
你要的程序已经调试好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-13 19:31:54 | 显示全部楼层
jackz007 发表于 2020-12-12 19:55
排序的要点是要对有先后次序的两个数做比较,合乎要求的维持原样,否则就交换位置。但是,在楼主的 ...

非常感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-13 19:32:29 | 显示全部楼层
风过无痕1989 发表于 2020-12-12 23:48
你要的程序已经调试好了

非常感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-14 05:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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