weixin_46889645 2020-04-09 10:24 采纳率: 0%
浏览 351

用scanf读入一句带有空格的英文以"End"结尾,计算出这句话中的单词个数,并且不算End,代码没有输出?

#include
#include
int main()
{
char ch[20];//This string has at most 99 characters and one character for '\0'.
int i=0;//Initialization.

int j;
printf("Please input a line of sentence: \n");//Input the string.
do
{
    scanf("%s",ch);
}
while(strcmp(ch,"End")!='\0');

if((ch[0]>='a'&&ch[0]<='z')||(ch[0]>='A'&&ch[0]<='Z'))//If the first character is a letter, then intialize j=1.
{
    j=1;
}
else//If the first character is not a letter, then intialize j=0.
{
    j=0;
}

for(i=1;i<100;i++)
{
    if(((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))&&(ch[i-1]==' '))//If the ith character is a letter and the previous one is a space.
               {
                    j++;//Then the number of the English words plus one.
               }
    if(ch[i-1]=='E'&&ch[i]=='n'&&ch[i+1]=='d')
    {
        j=j-1;
    }
}


printf("the output is:%d",j);//Output the result: the number of English words in a sentence.
return 0;

}

  • 写回答

1条回答 默认 最新

  • 辣椒日จุ๊บ 2020-04-09 10:59
    关注

    do
    {
    scanf("%s",ch);
    }
    while(strcmp(ch,"End")!='\0');
    把上面这段换成以回车结束输入的字符集
    scanf("%[^\n]",ch);

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。