How to Develop Word Counter Program in C++?

word-counter IN c++

1.) Write a program that takes words from a text file and prints each one on a separate line of an output file followed by the number of letters (alphabetic characters) in the word. Any leading or trailing punctuation marks should be removed from the word before it is printed. When all the text has been processed, display on the screen a count of the words in the file. Assume that words are groups of non-whitespace characters separated by one or more whitespace characters.

2.) Cooking recipes can be stored on a computer and, with the use of files, can be quickly referenced.

Write a function that will create a text file of recipes from information entered at the terminal. The format of the data to be stored is

-recipe type (dessert, meat, etc.)

-subtype (for dessert, use cake, pie, or cookies)

-name (e.g., German chocolate)

-number of lines in the recipe to follow

-the actual recipe

-Item 3 should be on a separate line.

Solution:

Count Words – Part 1

MAIN.CPP

#include “main.h”

 

int main(int argc, char *argv[])

{ clearFileContent();

 ifstream file(“Read.txt”);

     string str;

    while (std::getline(file, str))

    {

         

//remove punctuation

      for (int i = 0, len = str.size(); i < len; i++)

    {

        if (ispunct(str[i]))

        {

            str.erase(i–, 1);

            len = str.size();

        }

    }

      //   

             int i =0;

             //count words

             i =  CountWords(str);

            

              printWord(str);

             cout<<i;  cout<<“\n”;

    }

 

     int n;

    cin>>n;

   

    return EXIT_SUCCESS;

}

 

void clearFileContent(){

    

     std::ofstream ofs;

ofs.open(“Output.txt”, std::ofstream::out | std::ofstream::trunc);

ofs.close();

    

    

     }

 

  //printing

void printWord(std::string sentence)

{

  ofstream myfile (“Output.txt” ,  std::ios_base::app);

  if (myfile.is_open())

  {

     for(int i = 0; i < sentence.length(); i++)

  {

              if(sentence[i] == ‘ ‘) {myfile<<“\n”; cout << endl; }// check for whitespace

               else {cout << sentence[i] ;myfile <<sentence[i];}   

 

  }

  }   

     }

 

int CountWords(std::string strString)

{

  int nSpaces = 0;

  unsigned int i = 0;

 

  // Skip over spaces at the beginning of the word

  while(isspace(strString.at(i)))

    i++;

 

  for(; i < strString.length(); i++)

  {

    if(isspace(strString.at(i)))

    {

                              

      nSpaces++;

 

      // Skip over duplicate spaces & if a NULL character is found, we’re at the end of the string

      while(isspace(strString.at(i++)))

        if(strString.at(i) == ‘\0’)

          nSpaces–;

    }

  }

 

  // The number of words = the number of spaces + 1

  return nSpaces + 1;

}

 

MAIN.H

#include <cstdlib>

#include <iostream>

#include <fstream>

#include <string>

 

using namespace std;

void clearFileContent();

int CountWords(std::string strString) ;

void printWord(std::string sentence);

Part 2

MAIN.CPP

#include <cstdlib>

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

 

void clearFileContent();

 

void clearFileContent(){

    

     std::ofstream ofs;

ofs.open(“recipie.txt”, std::ofstream::out | std::ofstream::trunc);

ofs.close();

    

    

     }

int main(int argc, char *argv[])

 

{

    clearFileContent();

     ofstream myfile;

  myfile.open (“recipie.txt” ,std::ofstream::out | std::ofstream::trunc);

 

 std::string str;

     char type[100];  char subtype [100];char name  [100]; char actual[200];

     cout<< “recipe type : “;

   

      cin.getline(type,sizeof(type));

         str.append(“recipe type (“);

        str.append(type);

         str.append(“)”);

       myfile<<str;

       str = “”;

      

      cout<< “subtype  type : “;

      cin.getline(subtype,sizeof(subtype));

        str.append(“subtype (“);

       str.append(subtype);

         str.append(“)”);

       myfile<<str;

       str = “”;

    

       cout<< “name  : “;

      cin.getline(name ,sizeof(name ));

       str.append(“\nname (“);

       str.append(name);

         str.append(“)”);

       myfile<<str;

       str = “”;

        

      int n = 0;

      cout<< “number of lines in the recipe to follow : “;

     

      cin>>n;

      int i;

        str.append(“recipe steps “);

      for(i=0 ; i < n ; i++)

      {   

                  char follow[100];

                  

                   cin.getline(follow ,sizeof(follow ));

               str.append(“\n”);

                   str.append(follow);

                  

                   myfile<<str;

                str = “”;

              

              }

         cout<< “the actual recipe :  “;

          cin.getline(actual ,sizeof(actual ));

           str.append(“\nthe actual recipe  (“);

           str.append(actual);

             str.append(“)”);

         myfile<<str;

       str = “”;

    

        str.append(“\nactual recipe (“);

       str.append(actual);

         str.append(“)”);

       myfile<<str;

       str = “”;

         myfile.close();

    return EXIT_SUCCESS;

}

LEAVE A REPLY

Please enter your comment!
Please enter your name here