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

A map will easily give you the occurrences sorted by the key rather than the count, but you can easily use that to finish up the program:

#include <cctype>
#include <iostream>
#include <map>

int main()
{
    std::map<char, int> freq;
    char ch;

    while ( std::cin.get ( ch ) ) {
        if ( isalpha ( ch ) )
            ++freq[ch];
    }

    std::map<char, int>::const_iterator it = freq.begin();
    std::map<char, int>::const_iterator end = freq.end();

    while ( it != end ) {
        std::cout<< it->first <<": "<< it->second <<'\n';
        ++it;
    }
}

You won't read in a sentence with "cin>>". Use

#include <string>
//...
string str;
getline( cin, str);

Plus the a you used is an integer...

>> struct letteroccurance
>> {
>> char letters[];
>> int occurance[];
>> };
That won't do. Just make the counter 26 elements wide... You don't need the letters. Well not for counting. Consider this:

string str = "testing";
int let_count[26] = {0};

for ( size_t i=0; i<str.size(); i++ )
  let_count[str[i]-'a']++;

Thanks for the reply guys, but I think I'm more lost now than before, haha.

How 'bout this?

#include <string>
#include <iostream>
using namespace std;


struct letteroccurence
{
     char letter;
     int occurence;
};


int main ()
{
    string letter_box;
    letteroccurence occurences[26];   // 26 letters in alphabet
    cout << "Please enter a sentence ending with a period: ";

    
    // will read in string until you get a period.  Will throw out period.
    getline (cin, letter_box, '.');
    



/*
    Decide how you want to use the occurences array to store the letter tally.
    May need to set some counter for number of different letters that have shown up.
    If so, start by initializing that counter to 0.


    Put code to parse letter_box string letter by letter.
    For each character in letter_box, check occurences array to see whether that letter
    has occurred yet.  If it has, find the index of that occurences element and increment
    the occurence attribute for that index.  If not, initialize the next unused element of
    occurences to that letter and set the occurence attribute to 1.


*/




     return 0;
}

There are numerous ways to parse the string and keep track of how many letters there are. This gets the sentence you want (without the period), but that sentence will include non-letters as well. When parsing, you'll need to figure out whether the character is a letter. If not, throw it out and move to the next character in letter_box. If it is a letter, decide whether you have seen it or not and adjust occurences array accordingly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.