编写一个函数,接收一个字符串,统计其中数字字符个数及字母字符个数。函数原型为:void fun(const char *str, int* char_N, int* letter_N);
时间: 2023-12-29 22:02:11 浏览: 29
好的,这是一个简单的统计函数,以下是函数的实现:
```c++
void fun(const char *str, int* char_N, int* letter_N) {
*char_N = 0;
*letter_N = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (isdigit(str[i])) {
(*char_N)++;
} else if (isalpha(str[i])) {
(*letter_N)++;
}
}
}
```
这个函数接收一个字符串指针和两个整数指针作为参数,分别用于存储数字字符个数和字母字符个数。函数中使用了 `isdigit` 和 `isalpha` 函数来判断当前字符是数字字符还是字母字符,然后进行相应的计数。最后将计数结果存入指针所指向的变量中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)