74、下列程序的功能是:统计在TT字符串中'A'到'Z'26个字母各自现的次数,并存入
PP数组.请编写函数CNT(CHAR *TT,INT PP[])实现程序的要求,最后调用函数READWRITED
AT()把结果输出到文件OUT.DAT中.(仅统计小写字母.)
例如:当输入字符串:abcdefgabcdeabc后,输出的结果应该是:
3 3 3 2 2 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#include <conio.h>
#include <stdio.h>
void readwritedat();

void cnt(char *tt, int pp[])
{

}

main()
{
char tt[1000];
int pp[26], k, n;
clrscr() ;
printf("\nplease enter a char string:") ; scanf("%s",t
t);
cnt(tt,pp);
for(k = 0; k<26; k++) printf ("%d ",pp[k]);
printf("\n");
readwritedat();
}

void readwritedat()
{
char tt[1000];
int pp[26], k, n, i;
FILE *rf, *wf;

rf = fopen("in.dat","r");
wf = fopen("out.dat","w");
for(i = 0; i<10;i++)
{
fscanf(rf,"%s",tt);
cnt(tt,pp);
for(k = 0;k<26; k++) fprintf(wf,"%d",pp[k]);
fprintf(wf,"\n");
}
fclose(rf);
fclose(wf);
}

/* 注:题中必须先对数组pp赋初值。pp[*tt-'a']++的作用是将对应的元素增加1,即
当*tt为'a'时应对pp[0]加1,而pp[*tt-'a']即相当于pp[0],依此类推。*/
void cnt(char *tt, int pp[])
{
int i;
for(i=0;i<26;i++)  /*数组初始化*/
pp[i]=0;
while(*tt)
{if(*tt>='a'&&*tt<='z')
pp[*tt-'a']++;
tt++;
}
}

返回南开百题目录

www.163164.cn 联系QQ:3149886