Link to home
Start Free TrialLog in
Avatar of Shwn
Shwn

asked on

C++ question in indicating the number of occurrences of letters

Hi,
    I can't seem to figure out how I can write a program that reads several lines of text from the keyboard and prints a table indicating the number of occurrences of each letter of the alphabet in the text.  For example, the phrase

"I went to the store"

contains no "a's," no "b's," ....two "e's"..etc (alphabetically)

The following program is what I have:

#include <iostream.h>
#include <string.h>
 
void input();
void new_line();
const int size = 30;
 
int main()
{
      char string[size];
      char ans;
       
      do
      {
      cout<<"Please enter a line.\n";
      cin.getline(string, 30);
      cout<< string << endl;
      cout<< strlen(string) << endl;         
 
      cout<<"Yes/No\n";
      cin>> ans;
      new_line();
      }
      while((ans != 'n') && (ans != 'N'));
       return 0;
}

void new_line()
{
      char symbol;
      do
      {
            cin.get(symbol);
      }while(symbol != '\n');
}


Any types of help is appreciated

regards,
  shwn
Avatar of kgreddy
kgreddy

You please post the code if you have already tried the program.
I can tell you where you went wrong.

If you are new to programming altogether, refer to a C++
book and see what you need to do to accept text from the
key board.
After you accept the text from key board, you have to scan through
the text and keep a count of alphabets.

Avatar of Shwn

ASKER

Edited text of question.
ASKER CERTIFIED SOLUTION
Avatar of Resonance
Resonance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shwn

ASKER

Resonance,
    I like the idea of using switch and cases in your proposed program.  I've tried to incorporate your program into mine but it doesn't seem to assimilate correspondingly.  How would you go about writing the declaration and defining the class for the class of your program? Thanks in advance

Regards,
  Shwn
I think the use of seprate variables for this type of things is a bit unpleasant.

There is a data structure which is designed specifically for this type of application - called a map or associative array.  It allows you to associate a key (in your case the character) with a value (the count of characters).

Here is a version of the code that does something like what you want:

#include <iostream>
#include <map>
#include <string>

using namespace std;

// Need the pragma for Visual C++ :-(
#pragma warning(disable:4786)

void main()
{
    string line;

    map<char,int>  characterMap;

    cout << "Type some text, type END to stop." << endl;
    getline(cin,line);
    while (line != "END")
    {
        for (int i=0; i<line.length(); i++)
        {
            char thischar = line[i];
            if (tolower(thischar) >= 'a' && tolower(thischar) <= 'z')
            {
                if (characterMap.find(thischar) == characterMap.end())
                {
                                        characterMap[thischar] = 0;
                }
                ++characterMap[thischar];
            }
        }
        getline(cin,line);
    }

    map<char,int>::iterator i = characterMap.begin();
    while (i != characterMap.end())
    {
        cout << i->first << " = " << i->second << endl;
        ++i;
    }
}
I'd like to start by saying that Jason's solution is much more elegant than mine, and you should probably go with it.

However I'm a little confused about what you mean by "writing the definition and defining the class", since I wrote no functions, and created no new classes.