(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>
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;
	while (letterIndex <= 25) {
			while (stringCounter <= numberOfLetters){
				cout << letter[letterIndex];
				if (letter[letterIndex] == line[stringCounter]) {cout << "it worked";}

	letterIndex = 0;

	while (letterIndex < 26) {cout << counter[letterIndex] << endl;

	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.



In your count function, you have two basic approaches.

1 - Run through string 26 times, each time counting the occurrence of the corresponding letter. Keep track of the letter that has occurred the most.

2 - Run through the string once, incrementing counters stored in an array for each letter. Then run through the array looking for largest count.

Now, is the assignment to write one function, either taking a C-style string or a string object, whichever you prefer to implement? Or must you have a function(s) that will take either type, as the user chooses?

One thing you don't want to do, as you have, is store all the letters as separate strings. Your comparison code is trying to compare a string to a single char from the user's string. That shouldn't work.

If you want all the letters stored, here's a better approach, store them as an array of characters:

char letter[] = {'a', 'b', 'c' ...... 'z' };

Thank you very much. Changing

char *letter[] = {"a", "b", "c"... "z"};


char letter[] = {'a', 'b', 'c' ...... 'z' };

Seemed to do most of what I needed. I finally got it finished, thanks again for the help.