判断单词数

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=435 判断句子中有几个词; 句子中除了字母,标点,空格都是判断标志, 有时候会出现很多的标点,所以要设置一个变量来判断,如n

#include<stdio.h>

#include<string.h>

int main(){

   char s[1000];  

  int i,num,t,n;  

  while(gets(s)!=NULL){

   num=0;   

     n=0;   

     t=strlen(s);   

    for(i=0;i<t;i++)    {i

   if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z')

       n=1;   

       if(n)   

      if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z');   

     else {num++;n=0;}    }     printf("%d\n",num);         }  

   return 0;     }

其他人的:

01.#include <stdio.h> 

02.

  03.int main() 

04.{ 

05.    char c; 

06.    int flag = 1, count = 0; 

07.    while ((c = getchar()) != EOF) 

08. { 

09.        if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) 

10.        { 

11.            if (flag) 

12.            { 

13.                count++; 

14.                flag = 0; 

15.            } 

16.        } 

17.        else if (c == '\n') 

18.        { 

19.            printf("%d\n", count); 

20.            count = 0;

  21.            flag = 1;

  22.        } 

23.        else              flag = 1;      }      return 0;  } 

posted @ 2013-02-28 18:41  L kill  阅读(121)  评论(0编辑  收藏  举报