百练 2880:句中最长的单词

题目

以为要送到单词统计的方法来做,因为题中提示不要用while(),

#include "stdio.h"
main()
{
    char s[81],c;
    int i,num=0,word=0;
    printf("请输入一行英文:
");
    gets(s);
    for(i=0; (c=s[i])!=''; i++)
        if(c==' ') word=0;
        else if(word==0)
        {
            word=1;
            num++;
        }
    printf("本行中共有%d个单词.
",num);
}

然而,提示中说不需要while是指只有一行。用不用while还要看怎么做,用格式化输入函数scanf()也是一种选择。

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str[45],pstr[45];
    int len ,plen=0;
   while(~scanf("%s",str))
   {
        len = strlen(str);
       if(plen < len)
       {
           strcpy(pstr,str);
           plen = len;
       }
   }
   printf("%s
",pstr);
    return 0;
}

原文地址:https://www.cnblogs.com/qie-wei/p/10160209.html