OJ上一个单词统计问题
输入一行字符,统计其中有多少个单词。单词用空格分开。 需要注意的是,这里的单词是指一个或多个连续字母的组合。两个单词之间可以用若干个非连续字母字符分隔。如“a%3sd”就包含两个单词“a”和“sd”。
本题包含若干个测试案例。
对于每个测试案例,在单独一行输出。最后一行输出后,不要输出回车符。
数据输入样式:
I love coding.
hello
mm ai lao shu
数据输出样式:
There are 3 words in line 1;
There is 1 word in line 2;
There are 4 words in line 3.
=========================================
程序代码:#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main(){
char str[80],c;
int i,j=1,m=1,word,num,a[80],q=0;
while(gets(str)!=NULL)
{
word=0;
num=0;
for(i=0;(c=str[i])!='\0';i++)
{
if(isalpha(c)==0)
word=0;
else if(word==0)
{
word=1;
num++;
}
}
a[q]=num;
a[q+1]=j;
j++;
q=q+2;
}
q=q-1;
for(i=0;i<q;i=i+2)
if(a[i]==1&&i!=q-1)
printf("There is %d word in line %d;\n",a[i],a[i+1]);
else if(i!=q-1)
printf("There are %d words in line %d;\n",a[i],a[i+1]);
else if(i==q-1&&a[i]==1)
printf("There is %d word in line %d.",a[i],a[i+1]);
else
printf("There are %d words in line %d.",a[i],a[i+1]);
return 0;
}是不是OJ里不能用gets()函数的呀?那应该怎么用呢?请各位指点小弟一下。







