聿言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日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来