C++ Program: Counting letters,words,etc in input

#include <iostream>
#include <fstream>

int main()

{
std::string word;
std::getline(std::cin,word);
std::ofstream outf;
outf.open("lab3.txt");
outf<<word<<std::endl;
outf.close();
    std::ifstream lab3;
   
    lab3.open("lab3.txt");
    int countletters=0,countnum=0,countpunc=0,countspace=0,words=0,line=0;
    char character,prevchar = 0;
    if(!lab3)
    {
        std::cout << "Could not open file \n";
        return 1;
    }
    while(lab3.get(character) && !lab3.eof())
    {
        if(isalpha(character))
        {
            countletters++;
        }
       
if (isdigit(character))
        {
            countnum++;
        }
        if (ispunct(character))
         {
            countpunc++;
            if (isalpha(prevchar))
            {
               words++;
            }
        }
        if (isspace(character))
          {
            countspace++;
            if (isalpha(prevchar))
            {
               words++;
            }
          }
        if(character=='\n')
        {
            line++;
        }
        prevchar = character;
    }
    std::cout << "There are " << countletters << " letters.\n";
    std::cout << "There are " << countnum << " numbers.\n";
    std::cout << "There are " << countpunc << " punctuations.\n";
    std::cout << "There are " << countspace << " spaces.\n";
    std::cout << "There are " << words << " words.\n";
    std::cout << "There are " << line << " sentences.\n";
    lab3.close();
   
}
Detailed Question and Suggestion
Full question
Use of 'auto' keyword
stringstream 

You may also like:
C++: Check if the String is Palindrome
JAVA: Enhanced For Loop
What programming languages do what?
Which Linux distribution is the best for a programmer?
C++: Insertion Sort

Popular Posts