聿言2009 2022-04-21 21:02 采纳率: 100%
浏览 46
已结题

关于字符串的问题,如何解决?(语言-c++)

题目描述
输入一行单词序列,相邻单词之间由1个或多个空格间隔,请对应地计算各个单词的长度。

注意,如果有标点符号(如连字符,逗号),标点符号算作与之相连的词的一部分。没有被空格间开的符号串,都算作单词。

输入格式
一行单词序列,最少1个单词,最多300个单词,单词之间用至少1个空格间隔。单词序列总长度不超过1000。

输出格式
依次输出对应单词的长度,之间以逗号间隔。

样例
输入样例
She was born in 1990-01-02 and from Beijing city.
输出样例
3,3,4,2,10,3,4,7,5

问题代码:

#include<cstdio>
#include<cstring>
#include<iostream> 
using namespace std;
char a[1001];
int n,s; 
int main(){
    gets(a);//输入 
    n=strlen(a);//长度 
    for(int i=0;i<n;i++)
    {
        if(a[i]==' ')
        {
        if(s>0)
        printf("%d,",s);
        s=0;
        }
        else
        s++;
    }
    printf("%d",s);
    return 0;

75 Wrong Answer
foo.cc: In function 'int main()':
foo.cc:8:2: warning: 'char
gets(char
)' is deprecated [-Wdeprecated-declarations]
gets(a);//输入
^
In file included from /usr/include/c++/5/cstdio:42:0,
from foo.cc:1:
/usr/include/stdio.h:638:14: note: declared here
extern char gets (char __s) __wur attribute_deprecated;
^
foo.cc:8:2: warning: 'char
gets(char
)' is deprecated [-Wdeprecated-declarations]
gets(a);//输入
^
In file included from /usr/include/c++/5/cstdio:42:0,
from foo.cc:1:
/usr/include/stdio.h:638:14: note: declared here
extern char gets (char __s) __wur attribute_deprecated;
^
foo.cc:8:8: warning: 'char
gets(char
)' is deprecated [-Wdeprecated-declarations]
gets(a);//输入
^
In file included from /usr/include/c++/5/cstdio:42:0,
from foo.cc:1:
/usr/include/stdio.h:638:14: note: declared here
extern char *gets (char *__s) __wur attribute_deprecated;
^
/tmp/cct61iLt.o: In function main': foo.cc:(.text+0xe): warning: the gets' function is dangerous and should not be used.
识别到 4 个测试点。

状态 耗时 内存占用

#1 Accepted 2ms 256 KiB
#2 Wrong Answer 读取到 10,10,24,4,3,7,6...,应为 10,10,24,4,3,7,6...。 2ms 256 KiB
#3 Accepted 2ms 256 KiB
#4 Accepted 2ms 256 KiB
**

求改进

  • 写回答

1条回答 默认 最新

  • Code_流苏 C/C++领域优质创作者 2022-04-21 21:43
    关注

    你提交测试选的是C++的话,gets还是换一下吧
    可以考虑换成C++里的getline()函数。

    如有帮助,还请帮忙点下采纳!感谢!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 5月1日
  • 已采纳回答 4月23日
  • 创建了问题 4月21日

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行