编写一个名为countAlpha的函数,可以输出给定的字符串中出现次数最多的字母和出现的次数。

2025-03-04 12:21:44
推荐回答(1个)
回答1:

#include
using namespace std;
void countAlpha(char *str)
{
if(str==NULL || *str=='\0')
{
cout<<"Sorry, str is NULL"<return;
}

char c= 'A';
int t=0,tc=1;

for(int i=0; i{
tc=1;
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
for(int j=i+1; j{
if(str[i] == str[j] || str[i] == str[j]-('A'-'a') ||str[i] == str[j]+('A'-'a') )
{
tc++;
}
if(tc>t)
{
c = str[i];
if(c>='a'&&c<='z')
c = c-('a'-'A');
t = tc;
}
}
}

cout<<"The letter with largest number is:\n"<}
int main()
{
countAlpha("1232345974448993919040\n\t\b$%$^$#$@^@%@bcdaefgaddd");
countAlpha("ABCDEFGzZ");
countAlpha("1232345974448993919040\n\t\b$%$^");
countAlpha("\0");
countAlpha("");
system("pause");
return 0;
}