Search code examples
c++stringchartext-filesalphabet

How to check how many times the letters of the alphabet occur in a text file C++


I want to check how many times the letters of the alphabet are in a text file.

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>

using namespace std;


int main()
{
char alphabet[]="abcdefghijklmnopqrstuvwxyz";
//char alphabet[26]= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int letter_c[26]; //will contain the times each letter is repeated
string line;

//get text file
ifstream myfile ("hi.txt");

//if file opens...
  if (myfile.is_open())
  {
     while ( getline (myfile,line) )
     {  
         cout << line << '\n';

          //lowercase everything
          for (int i = 0; i < line.length(); i++)
          {
              line[i] = tolower(line[i]);
          }  

          //check if letter ? is in that text file and how many times
          for (int i = 0; i < line.length(); i++)
          {
               int count=0;
               for(int j=0; j<strlen(alphabet);i++)
               {
                   if(line[i]==alphabet[j])
                   {
                      cout<<"letter: alphabet "<<alphabet[j]<<endl;
                      cout<<"letter: text "<<line[i]<<endl;
                      letter_c[i]=count++;
                      //cout<<count<<endl;
                    }
               cout<<alphabet[i]<<" "<<letter_c[i]<<endl;
           }
      }
 }

//close file
myfile.close();
  }

  //file not found
  else cout << "Unable to open file"; 

return 0;

}

I believe this if statement is what is messing up my code:

if(line[i]==alphabet[j])
{
cout<<"letter: alphabet "<<alphabet[j]<<endl;
cout<<"letter: text "<<line[i]<<endl;
letter_c[i]=count++;
//cout<<count<<endl;
}

I have tried using line[i].compare(alphabet[j]) == 0 and I have also tried strcmp (line[i],alphabet[j]) == 0 and neither of them work.


Solution

  • You are over complicating the logic, instead just increase the count in letter_c at the found letter index, something like following :

    int letter_c[26] = {0};
    while ( myfile >> c )
    {
        if( isalpha(c) )
        {
            ++letter_c[ tolower(c) - 'a' ] ; // Change to lower case, subtract ascii of a
        }
    }