鱼C论坛

 找回密码
 立即注册
查看: 2176|回复: 4

字符数组

[复制链接]
发表于 2022-9-6 17:27:07 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5.         char str[3][20];
  6.         char string[20];
  7.         int i;
  8.         for(i=0;i<3;i++)
  9.                 gets(str[i]);
  10.         if(strcmp(str[0],str[1])>0)
  11.                 strcpy(string,str[0]);
  12.         else
  13.                 strcpy(string,str[1]);
  14.         if(strcmp(str[2],string)>0)
  15.                 strcpy(string,str[2]);
  16.         printf("\nthe largest string is:\n%s\n",string);
  17.         printf("%c\n",string[0]);
  18.         printf("%c",str[0]);
  19.         return 0;
  20. }
复制代码

改为用一维数组怎么写啊
#include<stdio.h>
#include<string.h>
int main()
{
        char a[20],b[20],c[20];
        gets(a);
        gets(b);
        gets(c);
        char str[20];
        if(strcmp(a,b)>0)
                strcpy(str,a);
        else
                strcpy(str,b);
        if(strcmp(c,str)>0)
                strcpy(str,c);
        printf("%s",str);
}
问题好像蛮多的,请教请教!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-6 17:31:44 | 显示全部楼层
你这不对么?感觉差不多的!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-6 17:32:11 | 显示全部楼层
桃花飞舞 发表于 2022-9-6 17:31
你这不对么?感觉差不多的!

运行有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-6 19:02:03 | 显示全部楼层
本帖最后由 jackz007 于 2022-9-6 19:07 编辑

        根本不要那么复杂,用指针很简单
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(void)
  4. {
  5.         char a[20] , b[20] , c[20] , * p                                                                    ;
  6.         printf("string a please : ")                                                                        ;
  7.         gets(a)                                                                                             ;
  8.         printf("string b please : ")                                                                        ;
  9.         gets(b)                                                                                             ;
  10.         printf("string c please : ")                                                                        ;
  11.         gets(c)                                                                                             ;
  12.         p = (strcmp(a , b) > 0 && strcmp(a , c) > 0) ? a : (strcmp(a , b) < 0 && strcmp(b , c) > 0) ? b : c ;
  13.         printf("\nthe largest string is : %s\n" , p)                                                        ;
  14. }
复制代码

        编译、运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. string a please : ABC
  4. string b please : DEF
  5. string c please : GHI

  6. the largest string is : GHI

  7. D:\[00.Exerciese.2022]\C>x
  8. string a please : abc
  9. string b please : XYZ
  10. string c please : 123

  11. the largest string is : abc

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

使用道具 举报

发表于 2022-9-7 21:11:16 | 显示全部楼层
你这个代码没问题啊,我在机器上试了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 23:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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