
(用C程序编写)输入一行字符,统计其中有多少个单词,单词之间用空格分隔开。
2个回答
展开全部
main()
{
int n=1;
char c;
printf("enter string:\n");
while((c=getchar())!='\n')
{
if(c==' ')++n;
}
printf("%d",n);
}
这个就是c语言的源代码。
注意:每输入一个空格就会认为输入一个单词,比如连续输入三个空格 会认为有三个单词。
{
int n=1;
char c;
printf("enter string:\n");
while((c=getchar())!='\n')
{
if(c==' ')++n;
}
printf("%d",n);
}
这个就是c语言的源代码。
注意:每输入一个空格就会认为输入一个单词,比如连续输入三个空格 会认为有三个单词。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
bool word=false;
int num=0;
while(*s){
if(word){
if(*s==' ' || *s=='\t'.....){word=false;num++;}
}
else{word=true;}
s++;
}
if(word){num++;}
int num=0;
while(*s){
if(word){
if(*s==' ' || *s=='\t'.....){word=false;num++;}
}
else{word=true;}
s++;
}
if(word){num++;}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询