您的位置:首页 > 其它

a program a day 10

2010-10-06 09:19 183 查看
/**
统计字符串中包含单词的个数
**/
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: