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

write a C++ program that reads an input file of text and builds a concordance of

ID: 670197 • Letter: W

Question

write a C++ program that reads an input file of text and builds a concordance of the words in the file. The program will also report the number of distinct words in the file. Also have the program seperated into a header file, class implementation file and a main file. Also have a working running test to prove that the program runs.

A class will contain the implementation of a concordance type as a linked list; each node of a list/concordance will contain a word and acount of the word's appearances in the input text, and these entries will be ordered alphabetically by their words. Any sequence of letters is a word, and all non-letter characters, including apostrophes, hyphens, and digits, are separators, equivalent to white space. One or more non-letters may separate words, and ends of lines separate words. Differences in capitalization do not make words different; that is, "HOUSE," "house," and "HouSe" are three instances of the same word. Words may be any length in the input file, but the program should consider only their first eight characters. Thus, "manipulated" and "manipulation" are two instances of the word "manipula."

OTHER REQUIREMENTS concordance.gif A class will implement a concordance abstract data type. Within the class, a concordance will be represented by a linked list whose links are pointers. Each node will contain a word, a count for that word, and a pointer, and the list will be ordered alphabetically by its words, in this way: concordance.gif I

n addition to the required constructor and destructor, implement only the following operations in this class: insert(word) - Inserts word in the invoking concordance in the correct position. If the word is already in the concordance, increment its count.

get_count(word) - Returns the count associated with word in the invoking concordance. This function returns zero if word is not in the concordance.

length() - Returns the length of the invoking concordance; that is, the number of distinct words that it lists.

Printing - Overloads the "<<" operator to print the invoking concordance to an output stream.

get_node(word,count,link) - Returns a pointer to a new node that contains word, count, and the pointer link. This function will be private.

Represent words using arrays of characters, as described below. INPUT The user will enter the name of the input file from the terminal. Any file of text is legitimate input for this program. Note that the program should be able to process its own source files without crashing. (These make interesting tests.) OUTPUT The program's output is a concordance of the words in the input file: a table of the words in alphabetical order accompanied by the number of times each word appears in the input text, as well as the number of distinct words in the text. The program prints this output to the terminal.

EXAMPLE
If an input file is this:
This is a small
test file, containing a small
number of words.
then the corresponding output might look something like this:
Word Count
---------------
A 2
CONTAINI 1
FILE 1
IS 1
NUMBER 1
OF 1
SMALL 2
TEST 1
THIS 1
WORDS 1
---------------
The file contains 10 distinct words.

The following code is missing some of the above information is present here:

//Header file//

#ifndef MYCONCORD_H
#define MYCONCORD_H
#include <bits/stdc++.h>
using namespace std;

class Node{
   public:
       string word;
       int count;
       Node* next;
       Node(string w,int c,Node* h);
};

class Concordance{
   private:
              Node* first;
   public:
       Concordance();
       bool contains(string wr);
       void insert(string word);
       void print();
};

#endif  

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

//myconcord.cpp//

#include "myconcord.h"
#include <bits/stdc++.h>
using namespace std;


Node::Node(string w,int c,Node* h){
   word = w;
   count = c;
   next = h;
}

Concordance::Concordance(){
   first = NULL;
}
//this function determines if the word is in the
bool Concordance::contains(string wr){
   Node* temp = first; //this makes the pointer temp equal to first
          //this while statment runs if temp is not equal to null
   while (temp != NULL){
       if (temp->word == wr){
           temp->count++;
           return true;
       }
       temp = temp->next;
   }
   return false;
}

void Concordance::insert(string word){
   if (contains(word) == false){
       Node* n = new Node(word,1,first);
       first = n;
   }
}

void Concordance::print(){
   Node *temp = first;
   while (temp != NULL){
       cout << temp->word << " " << temp->count << endl;
       temp = temp->next;
   }
}

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

//main.cpp//

#include "myconcord.h"
#include <bits/stdc++.h>
using namespace std;

int main(){
   ifstream infile;//this inizalizes a new file.
   string file_name; //this intializes the file inputed as a string
   cout << "Enter File Name : "; //this prompts the user to enter a file namd
   Concordance *c = new Concordance();
   cin >> file_name;
   string str,temp;
   infile.open(file_name.c_str());
   vector<string> Token;
   while (infile.eof() == false){
       getline(infile,str);
       Token.clear();
        stringstream line(str);
        while (line >> temp){ // Split the a string with spaces and store in a vector Token.
           Token.push_back(temp);
        }
        for (int i = 0; i < Token.size(); i++){
           string temp = "";
            for (int j = 0; j < Token[i].length(); j++){
               int res = (int)Token[i][j];
                if ((res >= 65 && res <= 90) || (res >= 97 && res <= 122)){
                   temp += (char)toupper(Token[i][j]);  
               }
           }
           c->insert(temp);
       }
   }
   c->print();
   return 0;
}

Explanation / Answer

This will be simple for you rather than creating struct separately..

#include<iostream.h>
#include<string.h>
char words[30][30];
//compare method
int compare(char p[],int size)
{
for(int i=0;i<size;i++)
{
if(strcmp(words[i],p)==0)
return 0;
}
return 1;
}
void main()
{
//variables declared
char data[100],second[20],ch,third[10][10];
int i=0,j,n=0,k=0,count,m;
clrscr();
cout<<" Enter any string ..for sentinel enter $::";
//Reading text from user
cin.get(ch);
do
{
data[n]=ch;
n++;
cin.get(ch);
}while(ch!='$');
data[n]='';
//comparing each words ...where space is a delimiter
for(;;)
{
for(j=0;(data[i]!=' ')&&(data[i]!='');j++)
{
second[j]=data[i];
i++;
}
second[j]='';
strcpy(third[k],second);
k++;
if(data[i]==0)
break;
i++;
}
//printing results
cout<<" ";
cout<<" word Frequency ";
cout<<" ------------------------------------ ";
for(i=0;i<k;i++)
{
count=1;
m=compare(third[i],i);
for(j=i+1;j<k;j++)
{
if(strcmp(third[i],third[j])==0 && m==1)
count++;
}
if(m==1)
{
strcpy(words[i],third[i]);
cout<<" "<<third[i]<<" "<<count<<endl;
}
}
}