输入一行字符,用函数编程统计其中有多少单词
时间: 2023-04-28 21:02:22 浏览: 278
可以使用Python中的split()函数将字符串按照空格分割成单词,然后统计单词的个数即可。
示例代码:
```python
def count_words(s):
words = s.split()
return len(words)
s = input("请输入一行字符:")
count = count_words(s)
print("单词个数为:", count)
```
输入:
```
请输入一行字符:Hello world! This is a test.
```
输出:
```
单词个数为: 6
```
相关问题
题目要求输入一行字符,用函数编程统计其中有多少单词。假设单词之间以空格分开,分别用字符数组和字符指针实现
好的,我可以帮你解决这个问题。
首先,我们可以定义一个函数,接收一个字符数组作为参数,然后统计其中的单词数。函数的实现如下:
```c
int countWords(char str[]) {
int count = 0;
int i = 0;
// 跳过字符串开头的空格
while (str[i] == ' ') {
i++;
}
// 计算单词数
while (str[i] != '\0') {
if (str[i] == ' ') {
count++;
// 跳过单词末尾的空格
while (str[i] == ' ') {
i++;
}
} else {
i++;
}
}
// 如果字符串末尾有单词,需要额外加上一个单词
if (str[i - 1] != ' ') {
count++;
}
return count;
}
```
接下来,我们可以编写一个简单的程序来测试这个函数:
```c
#include <stdio.h>
int countWords(char str[]);
int main() {
char str[100];
printf("请输入一行字符:\n");
gets(str);
int count = countWords(str);
printf("单词数为:%d\n", count);
return 0;
}
```
这个程序会提示用户输入一行字符,然后调用 `countWords` 函数来统计其中的单词数,并输出结果。
另外,如果你想用字符指针来实现这个函数,可以修改函数的参数为 `char*` 类型,然后在函数内部使用指针操作来访问字符数组。具体实现可以参考下面的代码:
```c
int countWords(char* str) {
int count = 0;
char* p = str;
// 跳过字符串开头的空格
while (*p == ' ') {
p++;
}
// 计算单词数
while (*p != '\0') {
if (*p == ' ') {
count++;
// 跳过单词末尾的空格
while (*p == ' ') {
p++;
}
} else {
p++;
}
}
// 如果字符串末尾有单词,需要额外加上一个单词
if (*(p - 1) != ' ') {
count++;
}
return count;
}
```
注意,在使用指针访问字符数组时,我们要使用 `*` 运算符来访问指针所指向的值,例如 `*p` 表示指针 `p` 所指向的字符。另外,指针也可以通过 `++` 运算符来实现指针的自增操作。
请分别使用字符数组和字符指针实现输入一行字符,用函数编程统计其中有多少个单词,假设单词之间用空格隔开
使用字符数组:
```c
#include <stdio.h>
#include <string.h>
int count_word(char str[]) {
int count = 0;
int is_word = 0;
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ' ') {
is_word = 0;
}
else if (!is_word) {
is_word = 1;
count++;
}
}
return count;
}
int main() {
char str[100];
printf("请输入一行字符:");
fgets(str, sizeof(str), stdin); // 使用 fgets 函数读入一行字符
int count = count_word(str);
printf("单词个数为:%d\n", count);
return 0;
}
```
使用字符指针:
```c
#include <stdio.h>
#include <string.h>
int count_word(char *str) {
int count = 0;
int is_word = 0;
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ' ') {
is_word = 0;
}
else if (!is_word) {
is_word = 1;
count++;
}
}
return count;
}
int main() {
char str[100];
char *p = str;
printf("请输入一行字符:");
fgets(str, sizeof(str), stdin); // 使用 fgets 函数读入一行字符
int count = count_word(p);
printf("单词个数为:%d\n", count);
return 0;
}
```
相关推荐
![](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)