C++ Programming Blog

 
 
 

Image

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <ctype.h>
 # include <conio.h>

 int main( )
    {
       clrscr( );

       fstream File(\"CP-14.txt\",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<\"Unable to open the input file.\"<<endl;
         cout<<\"Press any key to exit.\"<<endl;

         getch( );
         exit(EXIT_FAILURE);
      }

       char Data[100]={NULL};

       do
      {
         strset(Data,NULL);

         File.getline(Data,80,\'\\n\');

         if(strcmp(Data,NULL)==0)
        break;

         int frequency[52]={0};

         int length=strlen(Data);

         for(int i=0;i<length;i++)
        {
           if(isalpha(Data[i]))
              {
              if(isupper(Data[i]))
                 frequency[(int(Data[i])-65)]++;

              else if(islower(Data[i]))
                 frequency[(int(Data[i])-97+26)]++;
              }
        }

         int highest_frequency=0;

         for(int j=0;j<52;j++)
        {
           if(frequency[j]>highest_frequency)
              highest_frequency=frequency[j];
        }

         for(int k=0;k<52;k++)
        {
           if(frequency[k]==highest_frequency)
              {
             if(k>=0 && k<=25)
                cout<<char((k+65));

             else if(k>=26 && k<=51)
                cout<<char((k+97-26));
              }
        }

         cout<<\" \"<<highest_frequency<<endl;
      }
       while(1);

       File.close( );

       getch( );
       return 0;
    }