改错:C语言,输入一行字符,以回车结束,统计其中单词的个数。各单词之间用空格分隔,空格数可以是多个
程序填空,不要改变与输入输出有关的语句。输入一个正整数repeat(0<repeat<10),做repeat次下列运算:输入一行字符,以回车结束,统计其中单词的个数。各单...
程序填空,不要改变与输入输出有关的语句。
输入一个正整数repeat (0<repeat<10),做repeat次下列运算:
输入一行字符,以回车结束,统计其中单词的个数。各单词之间用空格分隔,空格数可以是多个。
输入输出示例:括号内是说明
输入
2 (repeat=2)
Let's go to room 209.
Programming is fun
输出
count = 5 ("Let's go to room 209."中有5个单词)
count = 3 (" Programming is fun"中有3个单词)
#include <stdio.h>
int main(void)
{
char c;
int count, word;
int repeat, ri;
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++){
c = getchar();
/*---------*/
printf("count = %d\n", count);
}
}
这是我的程序,出错,求帮忙改错
#include <stdio.h>
int main(void)
{
char c;
int count, word;
int repeat, ri;
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++){
c = getchar();
word=1;
count=0;
while(c!="\0"){
if(c!=" "&&word==1){
count++;
word=0;}
else if(c==" ") word=1;
c = getchar();
}
printf("count = %d\n", count);
}
}
思路是取字符,word变量作为标记符,count来计数。
附上思路图,求解答.. 展开
输入一个正整数repeat (0<repeat<10),做repeat次下列运算:
输入一行字符,以回车结束,统计其中单词的个数。各单词之间用空格分隔,空格数可以是多个。
输入输出示例:括号内是说明
输入
2 (repeat=2)
Let's go to room 209.
Programming is fun
输出
count = 5 ("Let's go to room 209."中有5个单词)
count = 3 (" Programming is fun"中有3个单词)
#include <stdio.h>
int main(void)
{
char c;
int count, word;
int repeat, ri;
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++){
c = getchar();
/*---------*/
printf("count = %d\n", count);
}
}
这是我的程序,出错,求帮忙改错
#include <stdio.h>
int main(void)
{
char c;
int count, word;
int repeat, ri;
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++){
c = getchar();
word=1;
count=0;
while(c!="\0"){
if(c!=" "&&word==1){
count++;
word=0;}
else if(c==" ") word=1;
c = getchar();
}
printf("count = %d\n", count);
}
}
思路是取字符,word变量作为标记符,count来计数。
附上思路图,求解答.. 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询