
一道C语言的题啊,~~请高手帮忙~~~~
描述从键盘输入一行字符,统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。输入输入只有一行句子。仅有空格和英文字母构成。输出单词的个数。样例输入stablemar...
描述
从键盘输入一行字符,统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。
输入
输入只有一行句子。仅有空格和英文字母构成。
输出
单词的个数。
样例输入
stable marriage problem Consists of Matching members
样例输出
7 展开
从键盘输入一行字符,统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。
输入
输入只有一行句子。仅有空格和英文字母构成。
输出
单词的个数。
样例输入
stable marriage problem Consists of Matching members
样例输出
7 展开
3个回答
展开全部
题呢 - -
追问
呵呵,补充了
追答
#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);
}
展开全部
#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));
}
#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));
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已经调试通过,望采纳!!!
#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);
}
#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);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询