Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that count the number of distinct words in a file. To make t

ID: 3628254 • Letter: W

Question

Write a C++ program that count the number of distinct words in a file. To make the problem easy, a word here means any string consisting of letters, symbols and numbers, words are separated from each other by the white spaces. So if you have the following, the number of words is 3.
Void main{ }
The words are also case sensitive. So “Hello hello” contains 2 distinct words, while “hello, hello” contains 1 distinct word

(1) You should allow the user to input the file name

(2) Don't have any assumptions about the number of words in the file, and the number of distinct words in the file. So you need to use the dynamic array as we discussed in the class.

(3) You don't need to sort the words in the array

(4) try to make your program clear and easy to read.

(5) You can discuss with others, but you should write your OWN code.

(6) Don't wait until the last day, try to submit it before the deadline. Submit your hard copy in class on the due date.

--------------

try to make comment for each step need a comment for me if that possible ?

Explanation / Answer

please rate - thanks

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
   {char filename[30];  
   string word;
int size =5;
string* array=new string[size];
  
   bool found;
   int i, count=0,newsize;
   cout<<"what is the name of the file you are using? ";
   cin>>filename;
   ifstream input;
   input.open(filename);           //open file
   if(input.fail())             //is it ok?
       { cout<<"file did not open please check it ";
        system("pause");
        return 1;
        }  
    cout<<"The words in the file ";
     input>>word;
     while(input)
        {cout<<word<<" ";
         found=false;
         for(i=0;i<count;i++)
              {
              if(word.compare(array[i])==0)
                    {found=true;
                     i=count;
                     }
              }
          if(!found)
              {
               if(count==size)
                   {newsize=size+1;
                   string *temp=new string[newsize];
                   for(i=0;i<size;i++)
                       temp[i]=array[i];
                    delete array;
                    array=temp;
                    size++;
                     }
                array[count]=word;
                count++;
               }
            input>>word;
            }
      cout<<" The "<<count<<" distinct words in the file are ";     
      for(i=0;i<count;i++)
           cout<<array[i]<<endl;
       input.close();
         system("pause");
         return 0;
     }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote