Search code examples
c++arrayscharacterasciialphabet

Count Alphabet Characters from an array?


So, in this program we have to

A. Count the number of each letter of the alphabet found in a character array and store these counts in an integer array

I understand that part but then it says we have to use the ASCII values which confuses me.

This is the file for reference:

 "   A frog walks into a bank and asks the teller, Miss Pattywack
     for a loan.  Miss Pattywack tells the frog,
     "you must have collateral for the loan".
     The frog pulls out of his pouch his porcelain people collection
     and offers them as collateral.
     Miss Pattywack looks at the figurines and tells the frog that
     the collection is not acceptable.
     The bank manager overhears the conversation and yells to the
     teller - "It's a nick nack Pattywack, give the frog a loan!"

Use a for loop to examine each character in the character array

i. Use toupper to case the letter, so you are only dealing with capital letters

ii. If the character is a letter of the alphabet, increment the integer array in the position of the ASCII value of the character minus 65

1) 65 is the ASCII value of letter, 'A'

(1) If the character is A, 65-65 = 0 the position you want to increment for the character A

(2) If the character is C, 67-65 = 2 the position you want to increment for the character C

I have this so far:

void CountAlphabetCharacters(char chArray[MAX_CHARACTERS], int lengthOfArray)
{
        int index;

        for(index = 0; index <= lengthOfArray; index++)
        {
            chArray[index] = toupper(chArray[index]);

               static_cast<int>(index);

        }   
}

That's all I have because that's all I understand. I mean, I understand how you get the ASCII value but I am so lost on how to actually make the for loop for this. Like I'm assuming you look at the characters from the file but I don't understand how you get that value you and keep going. I don't know if I make sense but I'm hoping I do and someone can help!! Thanks in advance.


Solution

  • Things you need:

    1. An array to hold the characters that you read. I think chArray is that array.
    2. An array to hold the count of the letters of the alphabet. I don't see that array.
    3. Update the code in CountAlphabetCharacters to go through the characters in chArray and update the count of letters in chArray.

    Before calling CountAlphabetCharacters, create the array for holding the count of characters.

    int charCountArray[26] = {0};
    

    Change the function CountAlphabetCharacters to accept it as an argument.

    void CountAlphabetCharacters(char chArray[MAX_CHARACTERS],
                                 int charCountArray[],
                                 int lengthOfArray)
    

    Use it as an argument in the call to CountAlphabetCharacters.

    Update the implementation of CountAlphabetCharacters.

    void CountAlphabetCharacters(char chArray[MAX_CHARACTERS],
                                 int charCountArray[],
                                 int lengthOfArray)
    {
       int index;
       int ch;
       for(index = 0; index <= lengthOfArray; index++)
       {
          // The character at the index.
          ch = chArray[index];
    
          // Check whether it is an alphabetical character.
          if ( isalpha(ch) )
          {
             // If so, make it the uppercase letter.
             ch = toupper(ch);
    
             // Increment the charCountArray for the letter.
             charCountArray[ch-'A']++;
          }
       }   
    }