程序,查找C ++中第二常见的字符
在本教程中,我们将讨论一个查找第二个最常见字符的程序。
为此,我们将提供一个字符串。我们的任务是仅将字符串迭代一次,然后在其中查找第二个最常见的字符。
示例
#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
//找到第二个最常出现的角色
char getSecondMostFreq(string str) {
int count[NO_OF_CHARS] = {0}, i;
for (i = 0; str[i]; i++)
(count[str[i]])++;
int first = 0, second = 0;
for (i = 0; i < NO_OF_CHARS; i++){
if (count[i] > count[first]){
second = first;
first = i;
}
else if (count[i] > count[second] &&
count[i] != count[first])
second = i;
}
return second;
}
int main(){
string str = "nhooo";
char res = getSecondMostFreq(str);
if (res != '\0')
cout << "Second most frequent char is " << res;
else
cout << "No second most frequent character";
return 0;
}输出结果
Second most frequent char is i
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练