| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 488 人关注过本帖
标题:OJ上一个单词统计问题
只看楼主 加入收藏
yjjlyyj
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-11-24
结帖率:100%
收藏
 问题点数:0 回复次数:1 
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()函数的呀?那应该怎么用呢?请各位指点小弟一下。


搜索更多相关主题的帖子: 统计 单词 
2010-11-24 14:33
shenhua2050
Rank: 2
等 级:论坛游民
帖 子:39
专家分:14
注 册:2010-11-23
收藏
得分:0 
#include<stdio.h>
#define N 80
char s1[N],s2[N],s3[N];
int i,count;
int edit(char *s)
{
       count=0;
    for(i=0;s[i]!='\0';i++)
    {
        if((s[i]<='z'&&s[i]>='a'))
        {
            count++;
            while((s[i]<='Z'&&s[i]>='A')||(s[i]<='z'&&s[i]>='a')) i++;
        }
    }
    return count;
}
 main()
{
    gets(s1);
    gets(s2);
    gets(s3);
    printf("There is %d word in line 1;\n",edit(s1));
    printf("There is %d word in line 2;\n",edit(s2));
    printf("There is %d word in line 3;\n",edit(s3));

}
2010-11-24 21:21
快速回复:OJ上一个单词统计问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015776 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved