I have the following program I need to write:
I need to read in a line of text and then output a list of all the letters that occur along with the number of times each letter occurs. An array with a struct type as its base must be used so it can hold both a letter and an int.
So if the program read in la lala laa. It would output:
Letter Occurance
a 5
l 4
The output needs to be sorted from high to low on how many times it occurred.
I have the following code so far:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char letter_box[20], next;
int index = 0;
cout << "Please enter in a sentence that ends with a period:\n";
cin >> next;
while (next != '.')
{
letter_box[index] = next;
index++;
cin >> next;
}
struct letteroccurance
{
char letters[];
int occurance[];
};
}
I imagine that during the while loop:
while (next != '.')
{
letter_box[index] = next;
index++;
cin >> next;
}
I could have some code to test and see if the letter is already in the array and if so add to the occurrence + 1. If it isn't enter the letter and set the occurrence to 1. I'm a bit stuck on how to do this though.
Any help would be most appreciated!
Edit/Delete Message