编写一个函数,接收一个字符串,统计其中数字字符个数及字母字符个数。函数原型为:void fun(const char *str, int* char_N, int* letter_N); 用C语言实现
时间: 2024-02-22 20:56:27 浏览: 28
下面是实现该函数的代码:
```c
#include <stdio.h>
#include <ctype.h>
void fun(const char *str, int* char_N, int* letter_N) {
*char_N = 0;
*letter_N = 0;
while (*str) {
if (isdigit(*str)) {
(*char_N)++;
} else if (isalpha(*str)) {
(*letter_N)++;
}
str++;
}
}
int main() {
int char_N, letter_N;
char str[] = "Hello 123 world!";
fun(str, &char_N, &letter_N);
printf("There are %d digit characters and %d letter characters.\n", char_N, letter_N);
return 0;
}
```
该函数使用指针作为参数,通过指针修改外部变量的值。在函数内部,使用 while 循环遍历字符串,使用 isdigit 和 isalpha 函数判断字符是数字字符还是字母字符,如果是数字字符则计数器 char_N 加 1,如果是字母字符则计数器 letter_N 加 1。函数执行完毕后,将结果保存到指针指向的变量中。最后在主函数中调用该函数并输出结果。
相关推荐
![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)
![](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)
![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)