| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 589 人关注过本帖
标题:统计单词个数,uva的简单题,真心不知道自己哪错了,给个错误样例~
只看楼主 加入收藏
cheng_nuo
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-1-26
结帖率:100%
收藏
已结贴  问题点数:1 回复次数:7 
统计单词个数,uva的简单题,真心不知道自己哪错了,给个错误样例~
这是我代码:
#include <stdio.h>
#include <string.h>
int main()
{
    char a[1000000];
    int count,len,i;
    //freopen("in.txt","r",stdin);
    while(gets(a) != NULL)
    {
      count=0;
      len=strlen(a);
      a[len+1]='\0';
      for(i=0; i<=len; i++)
      {
          if(a[i]>='A' && a[i]<='Z')
             a[i]+=32;
          if((a[i]>='a' && a[i]<='z') && (a[i+1]<'a' || a[i+1]>'z'))
            count++;
      }
      printf("%d\n",count);
    }
    return 0;
}
真心求解,谢~
搜索更多相关主题的帖子: 单词 统计 include count 
2013-01-26 12:17
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:0 
统计 单词 还是统计 字母?

My life is brilliant
2013-01-26 12:50
cheng_nuo
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-1-26
收藏
得分:0 
回复 2楼 lz1091914999
统计单词个数,这是uva上给的样例:
Sample Input

Meep Meep!
I tot I taw a putty tat.
I did! I did! I did taw a putty tat.
Shsssssssssh ... I am hunting wabbits. Heh Heh Heh Heh ...

Sample Output

2
7
10
9
2013-01-26 12:54
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:0 
给你一个思路,先从字符串首开始,遇到一个字母时,计数+1,然后从此处遍历至一个非字母字符或'\0'(结束)时为止,然后又继续找下一个字母。。。

My life is brilliant
2013-01-26 13:25
cheng_nuo
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-1-26
收藏
得分:0 
回复 4楼 lz1091914999
那我这样做哪错了?
2013-01-26 13:43
ksddah
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:53
专家分:135
注 册:2012-12-11
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#include<stdlib.h>

int main(void)
{
    char ch,prev;
    FILE *fp;
    char fname[20];
    int n_chars=0;
    int n_lines=0;
    int n_words=0;
    bool inword=false; 
    
    printf("Enter the name of the file: ");
    scanf("%s", fname);
    
    fp=fopen(fname,"r");
    if(fp==NULL)
     {
        printf("Failed to open file.\n");      
        exit(1);             
     }
    while((ch=getc(fp))!=EOF)
    {
        n_chars++;
        if(ch=='\n')
           n_lines++;
         if(!isspace(ch)&&!inword)
         {
           inword=true;
           if((tolower(ch)>='a')&&(tolower(ch)<='z'))
              n_words++;
         }
         if(isspace(ch)&&inword)
            inword=false;
    }
    printf("n_chars=%d,n_words=%d,n_lines=%d\n",n_chars,n_words,n_lines-1);
    return 0;   
}

Meep Meep!
I tot I taw a putty tat.
I did! I did! I did taw a putty tat.
Shsssssssssh ... I am hunting wabbits. Heh Heh Heh Heh ...
的输出为:
图片附件: 游客没有浏览图片的权限,请 登录注册
2013-01-26 13:50
xessupdown
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-1-26
收藏
得分:0 
首先求出的字符长度len,最后一项是len-1,即a[len+1]='0'这句可改为a[len]='0'.for语句一样的问题,至于关键语句 如果一直输入空格就惨了.关键语句错了

我给一个关键语句(当出现空格或者跳格符做个标记这里 用 point,后面的不是空格或者跳格那就是单词了 然后counter++)         
counter=0;//计数用
point=0;//作为标记
for(i=0;i<len;i++)
{
  if(a[i]==' ' ||a[i]=='\t' ||a[i]=='\n')
   {
     point=0;
   }
       else  if(point==0)//注意else if不可用if
   {
          counter++;
           point=1;
   }  
     
  
2013-01-26 18:45
xessupdown
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-1-26
收藏
得分:0 
简单的说就是空格符或者跳格符的后面跟的是单词,单词+1.再找空格跳格符。(因为单词之间是用空格或者跳格或者回车符隔开的)
2013-01-26 18:49
快速回复:统计单词个数,uva的简单题,真心不知道自己哪错了,给个错误样例~
数据加载中...
 
   



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

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