zoukankan      html  css  js  c++  java
  • 494

     Kindergarten Counting Game 

    Everybody sit down in a circle. Ok. Listen to me carefully.

    ``Woooooo, you scwewy wabbit!''

    Now, could someone tell me how many words I just said?

    Input and Output

    Input to your program will consist of a series of lines, each line containing multiple words (at least one). A ``word'' is defined as a consecutive sequence of letters (upper and/or lower case).

    Your program should output a word count for each line of input. Each word count should be printed on a separate line.

    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

    --------------------------------------------------------------------------------------------
    本题主要是判断一行字符串中有多少个word。判断方法为,一个字母开头到不是字母的字符结束为一个词。注意:也许一行的开头就是非字符
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<ctype.h>
     4 #define MAXN 1000
     5 char buf[MAXN];
     6 int main(){
     7     int len,j,sum;
     8     char oldch;
     9     while(fgets(buf,MAXN,stdin) != NULL){
    10         len = strlen(buf);
    11         sum = 0;
    12         oldch = buf[0];
    13         for(j = 1; j < len; j++){
    14            if(isalpha(oldch) > 0  && isalpha(buf[j]) == 0){
    15                 sum++;
    16             }
    17            oldch = buf[j];
    18         }
    19         printf("%d
    ",sum);
    20     }
    21 }

    刚开始犯的错误为:认为开端一定为字符,判断就使用了isalpha(j)  == 0 && isalpha(j-1) > 0,这导致开端为非字符的时候出现问题。

  • 相关阅读:
    企业库连接形式简单例子记录 EnterpriseLibrary.Data
    .net 抽象类(abstract)和接口(interface)区别
    windows service 开发、安装及调试
    asp.net(mvc) 框架
    获取IP地址
    select2 模糊查询远程数据
    设计模式篇——初探命令模式
    初探MVC路由
    初探表达式目录树
    C#实现插入排序法
  • 原文地址:https://www.cnblogs.com/fyymonica/p/4165837.html
Copyright © 2011-2022 走看看