简体   繁体   中英

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.

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
    }
}

You want std::map . Your key is the letter, and your value is the count.

Something like this should do what you want:

std::map<char, unsigned int> count;

std::fstream file("foo.txt");
char letter;

while(file >> letter)
{
    count[c] += 1;
}

If you want to treat uppercase and lowercase letters as the same, then use std::tolower :

count[std::tolower(c)] += 1;

This also has the added benefit of counting punctuation and special characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM