/*函数原型:int find_word(char const *dest)
*函数功能:查找字符串中的单词个数
*处理的异常情况:字符串有几个空格开始;单词之间有几个空格隔开;
*由几个空格结束等等
*/
#include<stdio.h>
#define INFO ((*(dest+1)>='a'&&*(dest+1)<='z')||(*(dest+1)>='A'&&*(dest+1)<='Z'))
int find_word(char const *dest)
{
int num = 0;
while(*dest == ' ')
{
dest++;
}
while(*dest != '\0')
{
if(*dest == ' ' && INFO)
num++;
dest++;
}
return num+1;
}
int main()
{
char *ch = " this is a test!";
printf("%d\n",find_word(ch));
return 0;
}
函数结果: