小编SU1*_*U15的帖子

字符串中字符的最大频率

#include<bits/stdc++.h>
#include<cstring>
#define arraySize 10

using namespace std;
char returnMaxOccur(char *str);

int main()
{
    char str[]="teet";
    cout<<"The max occuring character is"<<" "<<returnMaxOccur(str)<<endl;
    return 0;
}

char returnMaxOccur(char* str)
{
    int high=-1;
    char t;
    int counter[arraySize]={0};

    int len=strlen(str);

    for(int i=0;i<len;i++)
    {
        counter[str[i]]++;
    }


    for(int i=0;i<len;i++)
    {
        if(high<counter[str[i]])
        {
            high=counter[str[i]];
            t=str[i];
        }
    }
    return t;
}
Run Code Online (Sandbox Code Playgroud)

在下面的问题#include<bits/stdc++.h> 包含时 ,输入字符串的结果如下,

1)teet: ans is t  
2)eett: ans is e  
3)ttee: ans is t  
4)ette: ans is e  
Run Code Online (Sandbox Code Playgroud)

但当我包括#include<iostream> 而不是#include<bits/stdc++.h> 结果是

1)teet: …
Run Code Online (Sandbox Code Playgroud)

c++ string

4
推荐指数
1
解决办法
212
查看次数

标签 统计

c++ ×1

string ×1