鱼C论坛

 找回密码
 立即注册
查看: 1120|回复: 3

[已解决]c 语言

[复制链接]
发表于 2022-3-17 22:36:53 | 显示全部楼层 |阅读模式

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

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

x
//用二维数组存放每个单词

#include <stdio.h>

#define MAX  80

int main(void)
{
        char str[MAX+1];
        char *p=str;
        int cot=0,i=0,j=0;
    char ch;
   
        fgets(str,MAX,stdin);
        while(*p++!='\n')
        {
                if(*p==' ')
                {
                        cot++;       
                }       
        }
        char word[cot][30];

        p=str;
        while((ch=*p++)!='\n')
        {
                if(ch!=' ')
                {
                        word[i][j++]=ch;
                }
                if(ch==' ')
                {
                        word[i][j]='\0';
                        i++;
                        j=0;
                }
        }
        word[i][j]='\0';
       
        for(cot;cot>=0;cot--)
        {
                printf("%s",word[cot]);
                if(cot>0)
                {
            printf(" ");
                }
        }

        return 0;
}

我的思路是先创建一个二维数组,存好每个单词,最后根据最后一个单词没有句号输出,但是结果说我的程序运行时出现错误(如数组越界等问题),这是哪里出现了问题呢,请教各位大佬!
最佳答案
2022-3-18 06:59:45
本帖最后由 jhq999 于 2022-3-18 07:40 编辑
  1. #include <stdio.h>

  2. #define MAX  80

  3. int main(void)
  4. {
  5.         char str[MAX+1];
  6.         char *p=str;
  7.         int cot=0,i=0,j=0;
  8.         char ch;
  9.         
  10.         fgets(str,MAX,stdin);
  11.         while(*p++!='\n')
  12.         {
  13.                 if(*p==' ')
  14.                 {
  15.                         cot++;      
  16.                 }      
  17.         }
  18.                 cot++;//最后一个没有空格
  19.         char (*word)[30]=new char[cot][30];

  20.         p=str;
  21.         while((ch=*p++)!='\n')
  22.         {
  23.                 if(ch!=' ')
  24.                 {
  25.                         word[i][j++]=ch;
  26.                 }
  27.                 if(ch==' ')
  28.                 {
  29.                         word[i][j]='\0';
  30.                         i++;
  31.                         j=0;
  32.                 }
  33.         }
  34.         word[i][j]='\0';
  35.       
  36.         for(cot;cot>0;cot--)///////////
  37.         {
  38.                 printf("%s",word[cot-1]);
  39.                 if(cot>0)
  40.                 {
  41.             printf(" ");
  42.                 }
  43.         }

  44.         return 0;
  45. }
复制代码
  1. #include <stdio.h>

  2. #define MAX  80

  3. int main(void)
  4. {
  5.         char str[MAX+1];
  6.         int cot=0,i=0,j=0;
  7.         char ch;
  8.         
  9.         fgets(str,MAX,stdin);
  10.         while(str[cot]!='\n')
  11.         {
  12.                         cot++;           
  13.         }
  14.         str[cot]='\0';
  15.         for(cot;cot>0;cot--)
  16.         {
  17.                         if (str[cot]==0x20)
  18.                         {
  19.                                 printf("%s",str+cot+1);
  20.                                 printf("%c",0x20);
  21.                                 str[cot]='\0';
  22.                         }
  23.         }
  24.         printf("%s",str);
  25.         return 0;
  26. }
复制代码
  1. w eqrty u iop f gggh kl a
  2. a kl gggh f iop u eqrty w
复制代码

问题

问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-18 06:59:45 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-3-18 07:40 编辑
  1. #include <stdio.h>

  2. #define MAX  80

  3. int main(void)
  4. {
  5.         char str[MAX+1];
  6.         char *p=str;
  7.         int cot=0,i=0,j=0;
  8.         char ch;
  9.         
  10.         fgets(str,MAX,stdin);
  11.         while(*p++!='\n')
  12.         {
  13.                 if(*p==' ')
  14.                 {
  15.                         cot++;      
  16.                 }      
  17.         }
  18.                 cot++;//最后一个没有空格
  19.         char (*word)[30]=new char[cot][30];

  20.         p=str;
  21.         while((ch=*p++)!='\n')
  22.         {
  23.                 if(ch!=' ')
  24.                 {
  25.                         word[i][j++]=ch;
  26.                 }
  27.                 if(ch==' ')
  28.                 {
  29.                         word[i][j]='\0';
  30.                         i++;
  31.                         j=0;
  32.                 }
  33.         }
  34.         word[i][j]='\0';
  35.       
  36.         for(cot;cot>0;cot--)///////////
  37.         {
  38.                 printf("%s",word[cot-1]);
  39.                 if(cot>0)
  40.                 {
  41.             printf(" ");
  42.                 }
  43.         }

  44.         return 0;
  45. }
复制代码
  1. #include <stdio.h>

  2. #define MAX  80

  3. int main(void)
  4. {
  5.         char str[MAX+1];
  6.         int cot=0,i=0,j=0;
  7.         char ch;
  8.         
  9.         fgets(str,MAX,stdin);
  10.         while(str[cot]!='\n')
  11.         {
  12.                         cot++;           
  13.         }
  14.         str[cot]='\0';
  15.         for(cot;cot>0;cot--)
  16.         {
  17.                         if (str[cot]==0x20)
  18.                         {
  19.                                 printf("%s",str+cot+1);
  20.                                 printf("%c",0x20);
  21.                                 str[cot]='\0';
  22.                         }
  23.         }
  24.         printf("%s",str);
  25.         return 0;
  26. }
复制代码
  1. w eqrty u iop f gggh kl a
  2. a kl gggh f iop u eqrty w
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-18 09:44:46 From FishC Mobile | 显示全部楼层
  1. #include <stdio.h>

  2. void foo(){
  3.         char words[80];
  4.         if(scanf("%s", words) == 1)
  5.                 ;
  6.         else return;
  7.         foo();
  8.         printf("%s ", words);
  9. }

  10. int main(){
  11.         foo();
  12.         return 0;
  13. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-18 12:08:41 | 显示全部楼层

用递归只能实现反着输出,但是最后还是会有空格
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 21:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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