the letter e is the most frequently used letter in the english language, and the letter z is the lease used. a friend of yours doing a blah blah believes that this may not be necessarily be true of writings of first year college students. to test his theory, he asks you to write a program that will take a text file and print, for each letter of the english alphabet, the number of times the letter appears in the file

hint: use an integer array of size 128 and use the ASCII values of letters to index into the array to store and retrieve counts for the letters


Hello fellow programmers i have this question for a project and i just want to know if i follow the instructions correctly

i am a an international student that live in the us so the language is quit a barrier

#include <iostream>
#include <fstream> 
#include <string>
using namespace std;
const char FileName[] = "c:\\TestCount.txt";

int main()
{
   string lineBuffer;
   ifstream inMyStream (FileName); //open my file stream

   if ( inMyStream.is_open() )
   {
      //create an array to hold the counts
      int upperCaseCount[26] = {0};
      int lowerCaseCount[26] = {0};

      //read the text file
      while ( getline (inMyStream, lineBuffer) )
      {

		 //get a line of text
         getline (inMyStream, lineBuffer);

         //read through each  in the lineBuffer
         char oneLetter;
         for (int n=0; n < lineBuffer.length(); ++n )
         {
            oneLetter = char( lineBuffer[n] );      

            if ( oneLetter >= 'A' && oneLetter <='Z' )
            { //decide if it is a capital 
               upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array
            }

            if ( oneLetter >= 'a' && oneLetter <='z' )
            { //decide if it is a lower case
               upperCaseCount[int(oneLetter)- 97]++; //make the index match the count array
            }

		 }

         inMyStream.close(); //close the file stream
         //display the counts
         for ( int i = 0; i < 26; i++ )
		 {
            cout << char(i + 65) << "\t\t" << upperCaseCount[i] << endl;
		 }
         for ( int i = 0; i < 26; i++ )
		 {
            cout << char(i + 97) << "\t\t" << lowerCaseCount[i] << endl;
		 }
	  }
   }
   else
   {
		cout << "File Error: Open Failed\n";
   }
   
        
  return 0;
}

Recommended Answers

All 4 Replies

where did you follow the
hint: use an integer array of size 128 and use the ASCII values of letters to index into the array to store and retrieve counts for the letters?

By not using the hint, your code is much larger than it needs to be.

Can you give me more tips

Base on the code you posted, you are easily smart enough to rewrite it to use the hint.

Unless you didn't write that code. Did you?

yes i did, just wanted a second opinion

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.