第四题
请编写一个函数void fun(char *tt,int pp[]),统计在tt字符串中"a"到"z"26个字母各自出现的次数,并依次放在pp所指数组中。
例如,当输入字符串abcdefgabcdeabc后,程序的输出结果应该是:3 3 3 2 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
关于getchar()函数的用法,参考了下面这两篇文章,写的挺易懂的。
https://blog.youkuaiyun.com/qq_28238141/article/details/79927332
https://blog.youkuaiyun.com/happyforever91/article/details/51713741
/*请编写一个函数void fun(char *tt,int pp[]),统计在tt字符串中"a"到"z"26个字母各自出现的次数,并依次放在pp所指数组中。
例如,当输入字符串abcdefgabcdeabc后,程序的输出结果应该是:3 3 3 2 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
*/
#include <iostream.h>
#include <string.h>
#include <stdio.h> //getchar()需要引入stdio头文件
void fun(char *tt,int pp[])
{
char temp='a';
int res=0; //某个字母的个数
*tt = getchar();
while(*tt != '\n')
{
for(int n=0;n<26;n++)
{
if(*tt == temp+n)
{
//因为purchar()是一个字符一个字符的判断
//所以先从数组中把某个字母当前的次数拿出来赋值给res
res=pp[n];
//res自加
res++;
//再赋值给数组
pp[n]=res;
//还有一种更简单的写法:pp[n]+=1;
}
}
*tt=getchar();
}
//输出结果
for(int m=0;m<26;m++)
{
cout<<pp[m]<<" ";
}
cout<<endl;
}
void main()
{
char tt;
char *ptt=NULL;
ptt=&tt;
int pp[26]={0};
fun(ptt,pp);
}