(Most frequent character in a string)
I've been working on this problem for hours, and I'm just totally lost. I don't know if I'm heading in the right direction at all, and would really appreciate any help. My code isn't working at all, but I've included it.
The homework problem is this:
Write a function that accepts either a pointer to a C-string or a string object, as its argument. The function should return the character that appears most frequently in the string. Demonstrate the function in a complete program.
//This program takes in a pointer to a c-string and returns the character that appears most frequently
#include <iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
int letterIndex, stringCounter, highestNumber, highestLetter;
int counter[80];
char *letter[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
const int length = 80; // Maximum length for string
char line[length]; // Array of char
int numberOfLetters = 0, index = 0;
letterIndex = 0;
stringCounter = 0;
highestNumber = 0;
// Read a string into the character array.
cout << "Enter in a word or sentence less than "
<< length-1 << " characters:\n";
cin.getline(line, length);
numberOfLetters = strlen(line);
while (letterIndex < 26) {counter[letterIndex] = 0;
letterIndex++;}
while (letterIndex <= 25) {
while (stringCounter <= numberOfLetters){
cout << letter[letterIndex];
if (letter[letterIndex] == line[stringCounter]) {cout << "it worked";}
//counter[letterIndex]++;}
stringCounter++;}
letterIndex++;}
letterIndex = 0;
while (letterIndex < 26) {cout << counter[letterIndex] << endl;
letterIndex++;}
letterIndex = 0;
//while (letterIndex <= 25) {
// if (counter[letterIndex] > highestNumber) {
// highestNumber = counter[letterIndex];
// highestLetter = letterIndex;}
// letterIndex++;
// }
cout << "The letter you used most frequently was: " ;//<< letter[highestLetter] << ".\n";
return 0;
}
Like I said, I just have no idea where to go. Any help would be appreciated.
Thanks,
Terz