我想不出来。我的代码有什么问题?我是编程新手。
程序所需输出:编写一个C++程序,使用循环查找数组中出现最大值的字符。
我的代码:
#include <string.h>
using namespace std;
void FindMaxChar(char Word[])
{
int count = 0;
int max = 0;
char index = 0;
int length = strlen(Word);
for (int i = 0; i < length; i++)
{
index = Word[i];
for (int j = 0; j < length; j++)
{
if (index == Word[j])
{
count++;
}
}
if (count > max)
{
max = count;
index = Word[i];
}
}
cout << index << " is repeating " << max << " times.";
}
int main()
{
char Word[100] = {0};
cout << "Enter the Word = ";
cin.get(Word,100);
FindMaxChar(Word);
}
我的输出:
Enter the Word = caaar
r is repeating 11 times.
您从不在每个循环中重置
将
for (int i = 0; i < length; i++)
{
count = 0; // Reset counter
您还尝试将
相反,这里需要另一个变量。
还要注意,如果您将
cin.get(Word, 99);