/**
统计字符串中包含单词的个数
**/
#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;
}