I'm in an entry level c++ class. My professor wants us to write a array project that keep a frequency count of each of the italic alphas, also keep a frequency count of other characters entered (numbers and symbols).

The project output a frequency count of each italic alpha, and the others.
Determine which italic alpha occurred most often.
Output the stream entered in uppercase transposted form [A becomes B, B becomes C etc;].

Do not use any library functions.

He also mentions ASCII character set

I have only done the word count part, and it only count italic alpha (A-Z). It cannot count frequency of number and symbols. Can someone help me....thanks.

#include <iostream>
using namespace std;

void readAndCount (int &numWords, int letterCount[]);
void outputLetterCounts (int letterCount[]);

int main()
{
  int numWords;
  int letterCount[26] = {0};

  cout << endl;
  cout << "Enter a line of text.." << endl << endl;

  readAndCount (numWords, letterCount);

  cout << endl;
  outputLetterCounts(letterCount);
  cout << endl;

  return 0;
}

void readAndCount (int &numWords, int letterCount[])
{
  char character;
  int lastChar = 0; //Keeps track of previous entry.

  do 
  {
    if ( !cin.get ( character )) 
        break;

    if ( character >= 'a' && character <= 'z' )
        character -= 'a' - 'A';

    // Only works with upper case letters
    ++letterCount[character - 'A'];

    lastChar=isalnum(character);

  } 
  while ((character != '\n'));
}

void outputLetterCounts(int letterCount[])
{
  for (int i = 0; i < 26; i++)
    {
      if (letterCount[i] > 0)
    {
      cout << letterCount[i] << " " << char('a' + i) << endl;
    }
    }
}

Recommended Answers

All 3 Replies

by the wayz.....how can i set the maximum number of character to be 96.

>> how can i set the maximum number of character to be 96.

I assume you want to set the size of an array to be 96 as a maximum. To do that just place the value of 96 between the square brackets when you declare the array. However, a better way would be to declare a variable of type const int, call MAXCHAR and give it the value of 96. Throughout the program when you use MAXCHAR wherever you would have put 96. Then, at a later date if you want to change the maximum number of char to be 101 you just change the value of MAXCHAR and recompile rather than having to find each place you put 96.

If you mean you want to limit the number count of any char represented in the array of ints to 96 then I would look at the number of instances of the given char before I incremented it and if it is 96 already, then I wouldn't increment it. There may be other ways, but that's how I would do it.

// Only works with upper case letters
++letterCount[character - 'A'];

You could have an array of 128 ints and use the ASCII value of each char as the index. If you want to use the extended ASCII char set it would be 256 instead of 128. Either way you could do something like:

++letterCount[(int)character];

To be more precise about it you should probably use C++ casting syntax instead of C casting syntax. But, you might not even need to cast the character to int to do this. Try it yourself to see.

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.