write a C++ program that reads an input file of text and builds a concordance of
ID: 669425 • 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.
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."
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.
OTHER REQUIREMENTS
A class will implement a concordance abstract data type. Within the class, a concordance will be represented by alinked 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
In 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 beprivate.
Represent words using arrays of characters, as described below.
HINTS
This program will read its input from a file whose name the user enters.
C++ assumes that strings will be represented in arrays of characters. In an array of characters, the characters in a string occupy an initial segment of the array, and the null character '', marks the end of the string, though it is not itself a character in the string. Note that the null character is not the same as the pointer value NULL. An empty string---a string of length zero---is represented by an array of characters whose first position holds the null character. If you want to represent strings of a particular maximum length, as we do in this project, any arrays that will hold such strings must be one character longer than that maximum, to accomodate the terminating null character.
The library cstring contains several useful functions that operate on strings represented in this way. Note in particular:
strcpy(), which copies the contents of one string to another
strcmp(), which compares two strings alphabetically
strlen(), which returns the length of a string
Define the maximum word length as a constant in the Concordance class's header file, but before the definition section of the class. Define the word type in the definition section, and note that in the client program, it will be referred to like this: Concordance::Word. The definition file can begin like this:
const int MAX = 8;
class Concordance
{
public:
typedef char Word[MAX+1];
.
.
.
The nodes in the linked list hold a word, a count, and a pointer to the next node. The class's only data member is a pointer to such a node; for example:
struct Node
{
Word wd;
int count;
Node *next;
};
Node *first;
In the client program, build words one character at a time using a function called, say, read_word(). If the length of a word (in the input) is greater than eight, then this function will save only the first eight letters, but it will have to continue reading letters until it reaches the end of the input word; the program will discard letters beyond the first eight in each word. Don't forget to skip leading non-letters before a word begins, and terminate each word (string) with the null character.
The function strcmp(), which compares two strings, does so according to the ASCII values of their characters. To compare alphabetically, be sure that all characters are capitalized consistently: all upper-case or all lower-case.
Explanation / Answer
hai,
i write a code for something related to your question.
#include<iostream>
#include<string>
#define low 1
#define high 0
main()
{
int spaces, words, digits, vowels,special, a = high;
char *string;
spaces = words = digits = vowels = special = 0;
clrscr();
cout<<"Enter the input : ";
getline(cin,string);
while (*string != '')
{
if (*string == ' ')
{
a = high;
++spaces;
}
else if (a == high)
{
a = low;
++words;
}
if (isdigit(*string))
++digits;
if (isalpha(*string))
switch (*string)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
++vowels;
break;
}
if (!isdigit(*string) && !isalpha(*string))
++special;
string++;
}
cout<<" Number of words count %d";
cout<<words;
cout<<"Number of spaces %d";
cout<<spaces;
cout<<" Number of vowels %d";
cout<<vowels;
cout<<" Number of digits %d";
cout<<digits;
cout<<" Number of special characters %d";
cout<<special;
getch();
}
output:
Enter the string: This is a small
test file, containing a small
number of words.
Number of words count : 10
Number of spaces: ....
Likewise the above code will execute.
Thanks for asking queries.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.