用C语言编写一个函数,从键盘输入一个字符串,统计其中字母、数字、空格和其他字符的个数。在主函数中进行调用,并实现测试。
时间: 2024-05-10 19:20:53 浏览: 125
```
#include <stdio.h>
#include <ctype.h>
void count(char *str){
int letter = 0, digit = 0, space = 0, other = 0;
while(*str){
if(isalpha(*str)){
letter++;
}else if(isdigit(*str)){
digit++;
}else if(isspace(*str)){
space++;
}else{
other++;
}
str++;
}
printf("字母个数:%d\n", letter);
printf("数字个数:%d\n", digit);
printf("空格个数:%d\n", space);
printf("其他字符个数:%d\n", other);
}
int main(){
char str[100];
printf("请输入一个字符串:");
fgets(str, 100, stdin);
count(str);
return 0;
}
```
相关问题
用C语言编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串并在主函数中输出统计结果。 输入 任意一个字符串(长度小于80)。 输出 统计其中的英文字母、数字、空格以及其他字符的个数。
以下是C语言代码实现:
```c
#include <stdio.h>
#include <ctype.h>
void countChars(const char *str, int *letters, int *digits, int *spaces, int *others) {
while (*str) {
if (isalpha(*str)) {
(*letters)++;
} else if (isdigit(*str)) {
(*digits)++;
} else if (isspace(*str)) {
(*spaces)++;
} else {
(*others)++;
}
str++;
}
}
int main() {
char str[80];
int letters = 0, digits = 0, spaces = 0, others = 0;
printf("请输入一个字符串:");
gets(str); // 读取输入字符串
countChars(str, &letters, &digits, &spaces, &others);
printf("字母个数:%d\n", letters);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
printf("其他字符个数:%d\n", others);
return 0;
}
```
在该程序中,`countChars`函数接收一个指向字符串的指针和四个整型指针,分别用于记录字母、数字、空格和其他字符的个数。在函数中,使用`isalpha`、`isdigit`和`isspace`等标准库函数判断字符属于哪一类,然后逐个统计各类字符的个数。
在`main`函数中,先读取用户输入的字符串,然后调用`countChars`函数统计各类字符的个数,最后输出统计结果。
需要注意的是,该程序中使用了`gets`函数读取输入字符串,但是该函数存在安全漏洞,不建议在实际生产代码中使用。建议使用更加安全的`fgets`函数代替。
用C语言编写一个函数,统计字符串中字母数字空格和其他字符的个数,在主函数中调用该函数完成统计字符串输入以及结果输出
以下是用C语言编写的函数,可以实现统计字符串中字母、数字、空格和其他字符的个数:
```c
#include <stdio.h>
#include <ctype.h>
void count_chars(char *str, int *alpha, int *num, int *space, int *other) {
char c;
while (*str != '\0') {
c = *str++;
if (isalpha(c)) {
(*alpha)++;
} else if (isdigit(c)) {
(*num)++;
} else if (isspace(c)) {
(*space)++;
} else {
(*other)++;
}
}
}
int main() {
char str[100];
int alpha = 0, num = 0, space = 0, other = 0;
printf("请输入字符串:");
fgets(str, sizeof(str), stdin);
count_chars(str, &alpha, &num, &space, &other);
printf("字母个数:%d\n数字个数:%d\n空格个数:%d\n其他字符个数:%d\n", alpha, num, space, other);
return 0;
}
```
在主函数中,首先输入字符串,然后调用 `count_chars` 函数,将字符串和各个类型字符的计数器的指针作为参数传递给它。函数内部使用循环遍历字符串中的每个字符,判断它是字母、数字、空格还是其他字符,并累加相应的计数器。最后,在主函数中输出各个类型字符的个数。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.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)