编写一个函数名为countc函数,要求如下

来源:百度知道 编辑:UC知道 时间:2024/06/24 13:55:22
(1) 形式参数:array存放字符串的字符型数组名。
(2) 功能:统计array数组中大写字母的数目。
(3) 返回值:字符串中大写字母的数目。
编写一个C程序,输入一个字符串,调用countc函数,输出字符串中大写字母的个数

count[0-25]分别存储A-Z大写字母的个数。

#include <stdio.h>

int countc(char* instr, int count[])
{
int index = 0;
int i = 0;
int len =0;
int cnt = 0;

for (i=0; i < 25; i++)
{
count[i] = 0;
}

len = strlen(instr);
for(i = 0; i < len; i++)
{
index = instr[i] - 65;
if(index >= 0 && index < 26 )
count[index]++;
}

for(i = 0; i < 26; i++)
if(count[i] > 0) cnt = cnt + count[i];

return cnt;
}

void main()
{
char instr[1024];
int cnt [26] = {0};
int i;
int sum = 0;
scanf("%s", instr);
sum = countc(instr, cnt);

for (i=0; i < 26; i++)
printf("%c字母个数为:%d\n", (65 + i), cnt[i]);

printf("大写字母的个数为:%d\n", sum);

getchar();
exit(0);
}

上面直接可以编译通过!!