一道C语言的题啊,~~请高手帮忙~~~~

描述从键盘输入一行字符,统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。输入输入只有一行句子。仅有空格和英文字母构成。输出单词的个数。样例输入stablemar... 描述

从键盘输入一行字符,统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。

输入

输入只有一行句子。仅有空格和英文字母构成。

输出

单词的个数。

样例输入

stable marriage problem Consists of Matching members

样例输出

7
展开
 我来答
百度网友a9aac05dc
2011-12-21 · TA获得超过139个赞
知道小有建树答主
回答量:195
采纳率:0%
帮助的人:144万
展开全部
题呢 - -
追问
呵呵,补充了
追答
#include 
#include

void stat_str(const char *);

int main(void)
{
char str[1000];
gets(str);
stat_str(str);
return 0;
}

void stat_str(const char * str)
{
int words = 0,len = strlen(str);
int i = 0;
if(len)
words++;
while(i < len)
{
if(str[i++] == ' ')
{
while(i < len && str[i] == ' ')
i++;
if(i != len)
words++;
}
}
printf("%d\n",words);
}
leiyangbdwk
2011-12-21 · TA获得超过3296个赞
知道大有可为答主
回答量:4975
采纳率:12%
帮助的人:4689万
展开全部
#include <stdio.h>
#include <string.h>

int wordscount(char *p)
{
int c=0;

while(1)
{
while(*p++!=' ' && *p!='\0');
c++;
if (*p=='\0')
break;
while(*p++==' ');
}
return c;
}

void main()
{
char *s="aa bb c de d";
printf("%d\n",wordscount(s));
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
RABBITqyh
2011-12-21 · TA获得超过163个赞
知道小有建树答主
回答量:185
采纳率:33%
帮助的人:126万
展开全部
已经调试通过,望采纳!!!

#include <stdio.h>
#include <string.h>
void main()
{
int i, count=0, len;
char str[100];
gets(str);
len=strlen(str);
for (i=0;i<len;i++)
{
if(str[i]==' ' && str[i+1]!=' ')
count++;
}
if(str[i-1]!='\0' && str[i-1]!=' ')
count++;
printf("%d\n", count);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式