• UVA 494 Kindergarten Counting Game


    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
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char str[1000];
        int k,i,word,num;
        while(gets(str) && str[0]!=EOF)
        {
            k=strlen(str);
            word=1;num=0;
            for(i=0;i<k;i++)
            {
                if((str[i]>='A' && str[i]<='Z')||(str[i]>='a' && str[i]<='z'))
                {
                    if(word==1)
                    {
                      num++;
                      word=0;
                    }
                }
                else word=1;
            }
            printf("%d\n",num);
        }
        return 0;
    }
    
    
    
    
     
    #include<stdio.h>
    int main()
    {
        char ch;
        int word=1,count=0;
        while((ch=getchar())!=EOF)
        {
            if( (ch>='A' && ch<='Z') || (ch>='a' && ch<='z') )
            {
                if(word==1)
                {
                    count++;
                    word=0;
                }
            }
            else if(ch=='\n')
            {
                printf("%d\n",count);
                count=0;
                word=1;
            }
            else word=1;
        }
        return 0;
    
    }
    

    标记变量 单词出现时word==1, 否则word==0;

  • 相关阅读:
    多线程之volatile关键字
    多线程具体实现
    多线程的概述
    Linux基本目录机构
    Java13新特性
    CF1316D【Nash Matrix】(dfs+构造+思维)
    ego商城项目学习总结+出现问题及解决
    java.lang.OutOfMemoryError: GC overhead limit exceeded之tomcat7优化
    jsp在tomcat中更新不起作用
    js取值及赋值
  • 原文地址:https://www.cnblogs.com/liuzezhuang/p/2528611.html
Copyright © 2020-2023  润新知