C语言输入一行字符,统计其中有多少个单词,单词之间用空格分隔开

更新时间:2025-11-19 08:07:49
最佳答案

```c
include
include

int main() {
char input[1000]; // 假设输入的字符串不超过999个字符
bool inWord = false;
int wordCount = 0;

// 读取一行输入
fgets(input, sizeof(input), stdin);

// 遍历输入的字符串
for (int i = 0; input[i] != '\0'; i++) {
if (input[i] == ' ' && inWord) {
inWord = false;
} else if (input[i] != ' ' && !inWord) {
inWord = true;
wordCount++;
}
}

// 输出单词数量
printf("Total number of words: %d\n", wordCount);

return 0;
}
```

【考研刷题通】——你的考研刷题好帮手!无论是政治、英语,还是数学,【考研刷题通】小程序都能为你提供丰富的题目资源,助你轻松备战考研!快来加入我们,一起刷题提升,迈向成功之路!📚🎓【考研刷题通】——你的考研备考神器!

相关推荐

CopyRight © 2020-2025 考研攻略网 -考研各个学科复习攻略资料分享平台.网站地图 All rights reserved.

桂ICP备2022010597号-11 站务邮箱:newmikke@163.com

页面耗时0.0375秒, 内存占用1.55 MB, 访问数据库12次