函数void fun(char str[]intcount)的功能:统计str字符串中字母字符、数字字符、其他字符出现的次数,在程序划线部分填空完成此功能,说明程序中用数组名[下标]形式访问每一个元素。 如:输入串str:a#Bc 345&&*D 输出:436 #include <stdio.h> #include <string.h> #define L 256 #define N 3 void fun(char str[lint count) { int i=0; while(___[3]___!= 0) { if ((str[i]>='a'&& str[i]<='z')I(str[i] >='A'&&str[i]<='Z')) count[0]++; else if (___[4]___) count[1]++; else count[2]++; ___[5]__; }
时间: 2023-12-27 21:02:42 浏览: 40
#include <stdio.h>
#include <string.h>
#define L 256
#define N 3
void fun(char str[], int count[]) {
int i = 0;
while(str[i] != '\0') {
if((str[i]>='a'&& str[i]<='z')||(str[i] >='A'&&str[i]<='Z')) {
count[0]++;
} else if(str[i] >= '0' && str[i] <= '9') {
count[1]++;
} else {
count[2]++;
}
i++;
}
}
int main() {
char str[L];
int count[N] = {0};
printf("请输入字符串:\n");
fgets(str, L, stdin);
fun(str, count);
printf("字母字符:%d个\n数字字符:%d个\n其他字符:%d个", count[0], count[1], count[2]);
return 0;
}
在第一个下划线处填写 char,第二个下划线处填写 str[i] >= '0' && str[i] <= '9',第三个下划线处填写 i++。
相关问题
c语言编写一个函数,接收一个字符串,统计其中数字字符个数及字母字符个数。函数原型为:void fun(const char *str, int* char_N, int* letter_N);
好的,下面是代码:
```c
void fun(const char *str, int* char_N, int* letter_N) {
int num_count = 0;
int letter_count = 0;
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (isdigit(str[i])) {
num_count++;
} else if (isalpha(str[i])) {
letter_count++;
}
}
*char_N = num_count;
*letter_N = letter_count;
}
```
这个函数使用了循环遍历字符串中的每个字符,通过isdigit和isalpha函数来判断字符是数字还是字母。最后将数字字符个数和字母字符个数分别赋值给char_N和letter_N指针所指向的变量。
编写一个函数,接收一个字符串,统计其中数字字符个数及字母字符个数。函数原型为:void fun(const char *str, int* cha r_N, int* letter_N);
好的,以下是函数的实现:
```c++
void fun(const char *str, int* char_N, int* letter_N)
{
int digit_count = 0; // 记录数字字符个数
int letter_count = 0; // 记录字母字符个数
// 遍历字符串
for (int i = 0; str[i] != '\0'; i++)
{
// 如果是数字字符,digit_count 加一
if (isdigit(str[i]))
{
digit_count++;
}
// 如果是字母字符,letter_count 加一
else if (isalpha(str[i]))
{
letter_count++;
}
}
// 将结果存入指针指向的变量中
*char_N = digit_count;
*letter_N = letter_count;
}
```
该函数使用了两个指针参数,分别指向存储数字字符个数和字母字符个数的变量。函数内部使用了 `isdigit` 和 `isalpha` 函数来判断字符是否为数字或字母。最终将结果存入指针指向的变量中。