生无涯

吾生也有涯,而知也无涯,以无涯随有涯,乐以忘忧,生亦无涯矣www.cnblogs.com/shengwuya
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

a program a day 10

Posted on 2010-10-06 09:19  生无涯  阅读(177)  评论(0编辑  收藏  举报

/**
统计字符串中包含单词的个数
**/
#define MaxSize 100
#include<stdio.h>
int cWords(char * str)
{
 int count = 0,flag = 5;  //the initial value of flag should be larger than 2
 char * p = str;
 while(*p != '\0')
 {
  if(*p >= 'A' && *p <= 'Z'||*p >= 'a' && * p <= 'z')
   flag =1;
  else
   flag++;
  if(flag == 2)
   count++;
  p++;
 }
 if(flag == 1)          //the string doesn't ends with punctuation(标点)
  count++;
 return count;
}
int main()
{
 char str[] = "you are welcome!";
 printf("%s",str);
 printf("\n%d\n",cWords(str));

 char str1[] = "jia yu sheng"; //the string doesn't ends with punctuation(标点)
 printf("%s",str1);
 printf("\n%d\n",cWords(str1));

 char str2[] = "    you...are..welcome!";
 printf("%s",str2);
 printf("\n%d\n",cWords(str2));

 char str3[] = " ";
 printf("%s",str3);
 printf("\n%d\n",cWords(str3));
 return 1;
}