c语言编写一个函数,接收一个字符串,统计其中数字字符个数及字母字符个数。函数原
时间: 2023-12-29 10:00:29 浏览: 128
本可以如下编写:
```c
#include <stdio.h>
#include <ctype.h>
void countCharacters(char *str) {
int numCount = 0;
int letterCount = 0;
while(*str) {
if(isdigit(*str)) {
numCount++;
} else if(isalpha(*str)) {
letterCount++;
}
str++;
}
printf("数字字符个数:%d\n", numCount);
printf("字母字符个数:%d\n", letterCount);
}
int main() {
char input[100];
printf("请输入一个字符串:");
scanf("%s", input);
countCharacters(input);
return 0;
}
```
这个函数接收一个字符串作为参数,然后遍历这个字符串,通过isdigit()和isalpha()函数来判断其中的数字字符和字母字符,分别累加计数。最后在主函数中调用这个函数,输出统计结果。
相关问题
c语言编写一个函数,接收一个字符串,统计其中数字字符个数及字母字符个数
可以按照以下方式编写一个函数,接收一个字符串,统计其中数字字符个数及字母字符个数:
```c
#include <stdio.h>
#include <ctype.h>
void count_char(char *str, int *num_count, int *alpha_count) {
while (*str != '\0') {
if (isdigit(*str)) {
(*num_count)++;
}
else if (isalpha(*str)) {
(*alpha_count)++;
}
str++;
}
}
int main() {
char str[100];
int num_count = 0, alpha_count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
count_char(str, &num_count, &alpha_count);
printf("Number of digits: %d\n", num_count);
printf("Number of alphabets: %d\n", alpha_count);
return 0;
}
```
在上面的代码中,我们定义了一个名为 `count_char` 的函数,它接受三个参数:一个字符指针 `str`,以及两个整数指针 `num_count` 和 `alpha_count`。该函数使用 `isdigit` 和 `isalpha` 函数来检测字符串中的数字和字母字符,并将它们的数量分别累加到 `num_count` 和 `alpha_count` 中。
在 `main` 函数中,我们首先获取用户输入的字符串,并将其传递给 `count_char` 函数。然后,我们打印出数字和字母字符的数量。
c语言编写一个函数,统计一个输入字符串中字母、数字、空格和其他字符的个数
可以使用C语言中的字符函数库ctype.h中的函数来判断字符的类型。具体实现如下:
```c
#include <stdio.h>
#include <ctype.h>
void countChars(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[100];
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;
}
```
注意,上述代码使用了gets函数读入字符串,但是gets函数有安全性问题,可以使用fgets函数替代。同时,由于C语言中没有字符串类型,所以我们使用字符数组来表示字符串。
相关推荐
![](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)