Give this to your teacher. I don't think you'll understand everything although I did my best to keep it simple and straightforward. Just ask what you want to know.
By the way, I needed to split up theAlphabet initialization because something goes wrong with copying to this board. But you can make it just one line, or leave it like this.
Code:
#include <iostream>
#include <string>
using namespace std;
//Optional function: depends on IDE. void wait_for_enter();
//The world we are talking about. const unsigned CHARS = 52;
string theAlphabet = "abcdefghijklmnopqrstuvw" "qyzABCDEFGHIJKLMNOQRSTUVWXYZ";
unsigned theAlphabetCount[CHARS]={0};
string sSentence;
//Core functions. void menu();
void count_chars();
void print_stats();
int main()
{
menu();
count_chars();
print_stats();
wait_for_enter();
return 0;
}
//////////////////////////////////////////////// void wait_for_enter()
{
cout << "press <enter> to continue...";
// Reset failstate, just in case.
cin.clear();
string line;
getline( cin, line);
}
///////////////////////////////////////////////// void menu()
{
cout<<"Enter a sentence please (CTRL-Z/D to abort):"<<endl;
while( !(getline(cin, sSentence)) || sSentence.size()==0 )
{
//If CTRL-Z/D then exit: we don't want to enter anything. if(cin.eof())
break;
cout<<"Something went wrong, or you didn't enter a sentence. Please try again."<<endl;
//Just in case.
cin.clear();
}
}
///////////////////////////////////////////////// void count_chars()
{
for(unsigned i=0; i<sSentence.size(); ++i)
for(unsigned j=0; j<CHARS; ++j)
if( sSentence.substr(i,1) == theAlphabet.substr(j,1) )
theAlphabetCount[j]++;
}
////////////////////////////////////////////////// void print_stats()
{
cout<<"\t\tTHE STATISTICS"<<endl<<endl;
cout<<"\t\t--------------"<<endl<<endl;
cout<<"\t\tAnalized sentence: "<<sSentence<<endl<<endl;
cout<<"\t\tChar:\tCount:"<<endl;
for(unsigned i=0; i<CHARS; ++i)
if(theAlphabetCount[i] > 0)
cout<<"\t\t"<<theAlphabet[i]<<"\t"<<theAlphabetCount[i]<<endl;
}