求输入一个字符串,字符串里面有几个空格,用函数实现 c语言
时间: 2023-08-16 16:09:06 浏览: 110
可以使用以下代码实现:
```c
#include <stdio.h>
#include <string.h>
int count_spaces(char *str) {
int count = 0;
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] == ' ') {
count++;
}
}
return count;
}
int main() {
char str[100];
printf("请输入一个字符串:");
fgets(str, 100, stdin); // 从标准输入读取字符串
int spaces = count_spaces(str);
printf("字符串中有 %d 个空格\n", spaces);
return 0;
}
```
首先定义一个函数 `count_spaces`,它接受一个字符串参数 `str`,并返回该字符串中空格的数量。
在函数中,我们先定义一个计数器 `count`,初始值为0,然后遍历字符串中的每个字符,如果当前字符是空格,则将计数器加1。
最后在 `main` 函数中,从标准输入读取一个字符串,调用 `count_spaces` 函数统计空格数量,并输出结果。
相关问题
用C语言编写一个函数,实现输入一个字符串,可以求出字符串当中的大写、小写、数字、空格以及其他的字符
在C语言中,你可以创建一个函数来统计一个字符串中大写字母、小写字母、数字、空格和其他特殊字符的数量。这里是一个简单的示例:
```c
#include <stdio.h>
#include <ctype.h> // 包含处理字符类型的库函数
// 定义统计函数
void count_chars(char *str) {
int uppercase = 0;
int lowercase = 0;
int digits = 0;
int spaces = 0;
int others = 0;
while (*str != '\0') { // 遍历整个字符串
if (isupper(*str)) { // 检查大写字母
uppercase++;
} else if (islower(*str)) { // 检查小写字母
lowercase++;
} else if (isdigit(*str)) { // 检查数字
digits++;
} else if (*str == ' ') { // 检查空格
spaces++;
} else { // 其他字符(非字母、数字和空格)
others++;
}
str++; // 移动指针到下一个字符
}
printf("大写字母: %d\n", uppercase);
printf("小写字母: %d\n", lowercase);
printf("数字: %d\n", digits);
printf("空格: %d\n", spaces);
printf("其他字符: %d\n", others);
}
int main() {
char input[100];
printf("请输入一个字符串: ");
fgets(input, sizeof(input), stdin); // 输入字符串并去掉换行符
input[strlen(input)-1] = '\0'; // 结束字符串
count_chars(input); // 调用计数函数
return 0;
}
```
这个程序会提示用户输入一个字符串,然后统计其中的大写字母、小写字母、数字、空格以及其他字符的数量。
C语言编写一个函数,实现输入一个字符串,可以求出字符串当中的大写、小写、数字、空格以及其他的字符
您好!下面是一个使用C语言编写的函数,可以实现您所描述的功能:
```c
#include <stdio.h>
void analyzeString(const char* str) {
int uppercase = 0;
int lowercase = 0;
int digits = 0;
int spaces = 0;
int others = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
uppercase++;
} else if (str[i] >= 'a' && str[i] <= 'z') {
lowercase++;
} else if (str[i] >= '0' && str[i] <= '9') {
digits++;
} else if (str[i] == ' ') {
spaces++;
} else {
others++;
}
}
printf("大写字母数量:%d\n", uppercase);
printf("小写字母数量:%d\n", lowercase);
printf("数字数量:%d\n", digits);
printf("空格数量:%d\n", spaces);
printf("其他字符数量:%d\n", others);
}
int main() {
char str[100];
printf("请输入一个字符串:");
fgets(str, sizeof(str), stdin);
analyzeString(str);
return 0;
}
```
这个函数`analyzeString`接受一个字符串作为参数,并计算出其中大写字母、小写字母、数字、空格以及其他字符的数量。在`main`函数中,我们通过用户输入一个字符串,并调用`analyzeString`函数进行分析。最后,打印出各个类型字符的数量。
希望能解决您的问题!如果还有其他问题,请随时提问。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.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)