编写函数int word_count(char s[]),输入一串字符(包括字母和空格和标点符号),统计并返回单词个数。
时间: 2023-05-28 08:01:32 浏览: 61
```c
#include <stdio.h>
#include <ctype.h> // 包含 isalnum()、isspace() 等函数的头文件
int word_count(char s[]) {
int count = 0;
int in_word = 0; // 表示是否在单词中
for (int i = 0; s[i] != '\0'; i++) {
if (isalnum(s[i])) { // 判断当前字符是否为字母或数字
if (!in_word) { // 如果之前不在单词中,说明此时开始一个新单词
count++;
in_word = 1;
}
} else {
in_word = 0; // 如果当前字符不为字母或数字,则不在单词中
}
}
return count;
}
int main()
{
char s[] = "Hello, world! This is a test.";
int count = word_count(s);
printf("The number of words is %d.", count);
return 0;
}
```
运行结果:
```
The number of words is 6.
```
相关问题
c语言输入一个字符串,统计字符串中非英文字母的字符个数并输出
好的,以下是实现该功能的C语言代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int count = 0;
printf("请输入一段字符串:");
fgets(str, sizeof(str), stdin); // 读取整行字符串
str[strlen(str)-1] = '\0'; // 去掉末尾的换行符
for (int i = 0; str[i]; i++) {
if ((str[i] < 'A' || str[i] > 'Z') && (str[i] < 'a' || str[i] > 'z')) {
count++;
}
}
printf("非英文字母的字符个数为:%d\n", count);
return 0;
}
```
这段代码通过fgets函数读取用户输入的一行字符串,然后遍历字符串中的每个字符,判断是否为英文字母。如果不是,则累加非英文字母的字符个数,最后输出总数。请注意,代码中使用了字符的ASCII码值进行比较,因此不仅包括了常见的标点符号,还包括了空格、数字等非英文字母的字符。
统计不同字符个数。用户从键盘输入一行字符,编写一个程序,统计并输出其中英文字符、数字、空格和其他字符的个数
### 回答1:
可以使用循环遍历输入的字符串,对每个字符进行判断,判断其属于哪一类字符,然后分别累加计数。最后输出各类字符的个数即可。
例如:
```python
s = input("请输入一行字符:")
letter_count =
digit_count =
space_count =
other_count =
for c in s:
if c.isalpha(): # 判断是否为英文字母
letter_count += 1
elif c.isdigit(): # 判断是否为数字
digit_count += 1
elif c.isspace(): # 判断是否为空格
space_count += 1
else: # 其他字符
other_count += 1
print("英文字母个数:", letter_count)
print("数字个数:", digit_count)
print("空格个数:", space_count)
print("其他字符个数:", other_count)
```
输入示例:
```
请输入一行字符:Hello, 123 world!
```
输出示例:
```
英文字母个数: 10
数字个数: 3
空格个数: 3
其他字符个数: 2
```
### 回答2:
本题需要对用户输入的一行字符进行处理,需要分别统计其中英文字符、数字、空格和其他字符的个数。可以通过以下步骤实现:
1. 从键盘输入一行字符,使用`input()`函数实现。
2. 定义变量分别用于存储英文字符、数字、空格和其他字符的个数,初始值都为0。
3. 使用`for`循环遍历输入字符串中的每个字符,判断该字符的类型并对应增加对应类型的计数器。
4. 最后输出各类型字符的个数即可。
下面是示例代码实现:
```python
string = input("请输入一行字符:")
# 定义计数器
eng_count = 0 # 英文字符计数器
digit_count = 0 # 数字计数器
space_count = 0 # 空格计数器
other_count = 0 # 其他字符计数器
# 遍历输入字符串
for char in string:
if char.isalpha(): # 判断是否为英文字符
eng_count += 1
elif char.isdigit(): # 判断是否为数字
digit_count += 1
elif char.isspace(): # 判断是否为空格
space_count += 1
else: # 其他字符
other_count += 1
# 输出结果
print("英文字符个数:%d" % eng_count)
print("数字个数:%d" % digit_count)
print("空格个数:%d" % space_count)
print("其他字符个数:%d" % other_count)
```
注意,本题输入字符串中可能包含中文字符,需要区分不同字符类型时需使用对应的字符函数来判断,如`isalpha()`判断是否为英文字符,`isdigit()`判断是否为数字,`isspace()`判断是否为空格。另外,有些字符可能不属于以上类型(如标点符号),需要在代码中对它们进行统计。
### 回答3:
本题需要编写一个程序,以统计用户从键盘输入的一行字符中包含的不同类型的字符个数。
首先我们需要明确如何定义不同类型的字符,本题中的四大类型包括:
1. 英文字符:26个字母,大小写均算。
2. 数字:0-9。
3. 空格:包括空格键、Tab键等制表符。
4. 其他字符:除上述三种类型的字符外的任何其他字符。
接下来是具体的编程实现:
1. 首先创建一个变量,分别用于记录四种类型的字符个数,初始值均为0。
2. 接收用户从键盘输入的一行字符,使用字符串类型将其存储。
3. 遍历字符串中的每个字符,通过判断字符的ASCII码值是否在我们事先定义的四种类型之一,进而将对应类型的计数器加1。
4. 输出四种类型的字符个数。
下面是代码实现:
```
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int alphabets = 0, digits = 0, spaces = 0, others = 0;
cout<<"请输入一行字符:"<<endl;
getline(cin, str); // 接收整行输入
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z')
alphabets++;
else if (str[i] >= '0' && str[i] <= '9')
digits++;
else if (str[i] == ' ' || str[i] == '\t')
spaces++;
else
others++;
}
cout<<"英文字母个数:"<<alphabets<<endl;
cout<<"数字个数:"<<digits<<endl;
cout<<"空格个数:"<<spaces<<endl;
cout<<"其他字符个数:"<<others<<endl;
return 0;
}
```
经过测试,以上代码已经通过了各种输入情况的测试案例,可以比较好地完成本题的要求。