本科生进化 2023-02-03 19:31 采纳率: 33.3%
浏览 169
已结题

1375 :单词处理【C/C++/Java/Python】

题目描述
给定一个由英文字符、数字、空格和英文标点符号组成的字符串,长度不超过2000,请将其切分为单词,要求去掉所有的非英文字母,每行输出一个单词。
例如有文本:Python was created in 1990 by Guido van Rossum at Stichting Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands.
处理完成之后得到以下单词:

Python
was
created
in
by
Guido
van
Rossum
at
Stichting
Mathematisch
Centrum
CWI
see
http
www
cwi
nl
in
the
Netherlands
问题:在编程训练系统中测评完成率只有10%,我在VS上运行时得出的结果是正常的,想知道是哪里出的问题。********
我的代码:

#include"stdio.h"
int hanshu(char ch)
{
    if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int main()
{
    char ch[2000];
    fgets(ch, 2000, stdin);
    int i = 0;
    int flag = 0;
    int f = 1;
    while (ch[i] != '\n')
    {
         if (hanshu(ch[i])==1)
        {
            printf("%c", ch[i]);
            f = 0;
          
        }
        else if(f==0&&hanshu(ch[i])==0)
        {
             flag = 1;
            
        }
         if (flag == 1 && f == 0)
         {
             if (ch[i + 1] != '\n')
             {
                 printf("\n");
                 flag = 0;
                 f = 1;
             }
         }
         i++;
    }
    return 0;
}

  • 写回答

2条回答 默认 最新

  • 本科生进化 2023-02-03 20:47
    关注

    正确答案

    #include <stdio.h>
    #include <stdlib.h>
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int main(int argc, char *argv[]) {
        int h=0,i;
        char ch[2000];
        gets(ch);
        for(i=0;ch[i]!='\0';i++){
            if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))
            {
                if(h!=0)
                {
                    printf("\n");
                    h=0;
                }
                printf("%c",ch[i]); 
            }
            else
            {
                h++;
            }
        }
        
    
        
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月3日
  • 已采纳回答 2月3日
  • 修改了问题 2月3日
  • 创建了问题 2月3日

悬赏问题

  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'