C语言用户每输入一行字符,程序识别一行字符中单词的个数并输出,直到用户输入空行为止代码
时间: 2023-09-09 17:05:11 浏览: 58
### 回答1:
我提供一个简单的C语言代码,可以实现上述功能:#include <stdio.h>
#include <string.h>int main(void)
{
char str[100];
while (fgets(str, sizeof(str), stdin) != NULL && strcmp(str, "\n") != 0)
{
int count = 0;
char *word = strtok(str, " ");
while (word != NULL)
{
count++;
word = strtok(NULL, " ");
}
printf("行中单词的个数是:%d\n", count);
}
return 0;
}
### 回答2:
代码如下:
```c
#include <stdio.h>
int main() {
char line[100]; // 定义一个字符数组,用于存储用户输入的一行字符
int wordCount = 0; // 记录单词个数
while (1) {
printf("请输入一行字符:");
gets(line); // 获取用户输入的一行字符
if (line[0] == '\0') { // 如果用户输入了空行,则退出循环
break;
}
int i = 0;
int isWord = 0; // 是否正在输入单词
while (line[i] != '\0') {
if (line[i] != ' ' && line[i] != '\n') { // 如果当前字符不是空格和换行符,则判断为单词
if (!isWord) { // 如果之前不是正在输入单词,则单词计数加一
wordCount++;
isWord = 1;
}
} else { // 如果当前字符是空格或换行符,则不是单词
isWord = 0;
}
i++;
}
}
printf("单词个数为:%d\n", wordCount);
return 0;
}
```
这段代码使用了一个字符数组`line`来存储用户输入的一行字符,然后通过遍历该字符数组来判断单词的个数。在遍历过程中,如果当前字符不是空格或换行符且之前不是正在输入单词,则将单词计数加一。最后输出单词的个数。
用户可以连续输入多行,直到输入了空行才停止。
### 回答3:
可以用以下的C语言代码实现该功能:
```c
#include <stdio.h>
#include <stdbool.h> // 包含bool类型,用于条件判断
int main() {
char sentence[100]; // 保存用户输入的一行字符的数组
int wordCount = 0; // 单词计数器
printf("请输入一行字符(输入空行结束):\n");
// 使用do-while循环,先执行一次循环,再进行判断
do {
fgets(sentence, sizeof(sentence), stdin); // 从标准输入读取一行字符,并保存到sentence数组中
int i = 0;
bool isWord = false; // 是否处于单词中的状态
// 遍历一行字符,根据空格和换行符进行判断
while (sentence[i] != '\0') {
// 如果当前字符是字母,并且前一个字符不是字母,说明遇到了新的单词
if (isalpha(sentence[i]) && !isWord) {
wordCount++;
isWord = true; // 设置为处于单词中的状态
}
// 如果当前字符是空格或换行符,并且处于单词中的状态,说明单词结束
else if ((sentence[i] == ' ' || sentence[i] == '\n') && isWord) {
isWord = false; // 设置为不处于单词中的状态
}
i++;
}
} while (sentence[0] != '\n'); // 若输入的是空行,则结束循环
printf("共输入了 %d 个单词。\n", wordCount);
return 0;
}
```
该程序使用`fgets`函数从标准输入读取一行字符,并保存在`sentence`数组中。然后,使用一个循环遍历`sentence`数组中的每个字符,根据空格和换行符来判断单词的起始和结束位置。如果当前字符是字母,并且前一个字符不是字母,说明遇到了新的单词,单词计数器就加1。如果当前字符是空格或换行符,并且处于单词中的状态,说明单词结束,将状态设置为不处于单词中。当用户输入空行时,循环结束,程序输出单词的个数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)