Question

Write a program, called wordcount.c, that reads one word at a time from the standard input....

Write a program, called wordcount.c, that reads one word at a time
from the standard input. It keeps track of the words read and the
number of occurrences of each word. When it encounters the end of
input, it prints the most frequently occurring word and its count.
The following screenshot shows the program in action:


adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a
sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is so.

The most frequent word is "is", which appears 3 times. adminuser@adminuser-virtualbox ~/Desktop/HW8 $ wordCount < mobydick.txt

The most frequest word is "the", which appears 13666 times

The first time I called wordcount, I entered two lines of text as input,
followed by a <CTRL-D>. (When entering text from the keyboard, typing <CTRL-D> at
the beginning of a line signals end-of-input.) The line beginning
"The most frequent word..." is the output. The second time I called
the program, I used redirection (<) to have the file
mobydick.txt be treated as the standard input.

Your program should consider a "word" to be a sequence of non-blank characters, as
retrieved by the "%s" format in scanf. No need to remove
punctuation or turn uppercase letters into lower-case ones. For
example, "is", "Is", and "is." are all different words.

Your program should store the word information in a binary search tree.
The file bst.h gives the definitions of the BSTnode structure, and
defines some functions you should implement. In particular, a node
contains a pointer to an item, plus a pointer to the left and right
child nodes. The item is a (void *) pointer, which means that the
node is able to store anything. You should write a file bst.c to
implement these functions.

The program wordcount.c will use a BST that has one node in the tree for each distinct word. The item for
the node should be a pointer to a WordCount structure, defined as follows:

struct wordcount {

char word[80];

int count;

};

typedef struct wordcount WordCount;

bst.h provided below // bst.h:
Functions to support binary search trees #ifndef BST_H #define
BST_H struct bstnode { void *item; struct bstnode *left; struct
bstnode *right; }; typedef struct bstnode BSTnode; // Create a new
node having the specified item. BSTnode *createNode(void *item); //
Search the tree rooted at node n for the node matching the
specified item. // If the item exists, return its node. If not,
return the node that would be its parent. // The function "compare"
compares items. BSTnode *find(BSTnode *n, void *item, int
compare(void *, void *)); // Insert an item into the tree rooted at
node n. // If the tree already has a node for that item, do
nothing. void insert(BSTnode *n, void *item, int compare(void *,
void *)); #endif Show transcribed image text

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <string.h>
#include <ctype.h>
# define MAX_CHARS 26
# define MAX_WORD_SIZE 30
struct TrieNode
{
bool isEnd;
unsigned frequency;
int indexMinHeap;
TrieNode* child[MAX_CHARS];
};
struct MinHeapNode
{
TrieNode* root;
unsigned frequency;
char* word;
};
struct MinHeap
{
unsigned capacity;
int count;
MinHeapNode* array;
};
TrieNode* newTrieNode()
{
TrieNode* trieNode = new TrieNode;
trieNode->isEnd = 0;
trieNode->frequency = 0;
trieNode->indexMinHeap = -1;
for( int i = 0; i < MAX_CHARS; ++i )
trieNode->child[i] = NULL;
return trieNode;
}
MinHeap* createMinHeap( int capacity )
{
MinHeap* minHeap = new MinHeap;
minHeap->capacity = capacity;
minHeap->count =0;
minHeap->array = new MinHeapNode [ minHeap->capacity ];
return minHeap;
}
void swapMinHeapNodes ( MinHeapNode* a, MinHeapNode* b )
{
MinHeapNode temp = *a;
*a = *b;
*b = temp;
}
void minHeapify( MinHeap* minHeap, int idx )
{
int left, right, smallest;
left = 2 * idx + 1;
right = 2 * idx + 2;
smallest = idx;
if ( left < minHeap->count && minHeap->array[ left ]. frequency < minHeap->array[ smallest ]. frequency)
smallest = left;
if ( right < minHeap->count && minHeap->array[ right ]. frequency < minHeap->array[ smallest ]. frequency)
smallest = right;
if( smallest != idx )
{
minHeap->array[ smallest ]. root->indexMinHeap = idx;
minHeap->array[ idx ]. root->indexMinHeap = smallest;
swapMinHeapNodes (&minHeap->array[ smallest ], &minHeap->array[ idx ]);
minHeapify( minHeap, smallest );
}
}
void buildMinHeap( MinHeap* minHeap )
{
int n, i;
n = minHeap->count - 1;
for( i = ( n - 1 ) / 2; i >= 0; --i )
minHeapify( minHeap, i );
}
void insertInMinHeap( MinHeap* minHeap, TrieNode** root, const char* word )
{
if( (*root)->indexMinHeap != -1 )
{
++( minHeap->array[ (*root)->indexMinHeap ]. frequency );
minHeapify( minHeap, (*root)->indexMinHeap );
}
else if( minHeap->count < minHeap->capacity )
{
int count = minHeap->count;
minHeap->array[ count ]. frequency = (*root)->frequency;
minHeap->array[ count ]. word = new char [strlen( word ) + 1];
strcpy( minHeap->array[ count ]. word, word );
minHeap->array[ count ]. root = *root;
(*root)->indexMinHeap = minHeap->count;
++( minHeap->count );
buildMinHeap( minHeap );
}
else if ( (*root)->frequency > minHeap->array[0]. frequency )
{
minHeap->array[ 0 ]. root->indexMinHeap = -1;
minHeap->array[ 0 ]. root = *root;
minHeap->array[ 0 ]. root->indexMinHeap = 0;
minHeap->array[ 0 ]. frequency = (*root)->frequency;
delete [] minHeap->array[ 0 ]. word;
minHeap->array[ 0 ]. word = new char [strlen( word ) + 1];
strcpy( minHeap->array[ 0 ]. word, word );
minHeapify ( minHeap, 0 );
}
}
void insertUtil ( TrieNode** root, MinHeap* minHeap,const char* word, const char* dupWord )
{
if ( *root == NULL )
*root = newTrieNode();
if ( *word != '\0' )
insertUtil ( &((*root)->child[ tolower( *word ) - 97 ]), minHeap, word + 1, dupWord );
else
{
if ( (*root)->isEnd )
++( (*root)->frequency );
else
{
(*root)->isEnd = 1;
(*root)->frequency = 1;
}
insertInMinHeap( minHeap, root, dupWord );
}
}
void insertTrieAndHeap(const char *word, TrieNode** root, MinHeap* minHeap)
{
insertUtil( root, minHeap, word, word );
}
void displayMinHeap( MinHeap* minHeap )
{
int i;
for( i = 0; i < minHeap->count; ++i )
{
printf( "%s : %d\n", minHeap->array[i].word,minHeap->array[i].frequency );
}
}
void printKMostFreq( FILE* fp, int k )
{
MinHeap* minHeap = createMinHeap( k );
TrieNode* root = NULL;
char buffer[MAX_WORD_SIZE];
while( fscanf( fp, "%s", buffer ) != EOF )
insertTrieAndHeap(buffer, &root, minHeap);
displayMinHeap( minHeap );
}
int main()
{
int k = 5;
FILE *fp = fopen ("HomeworkLib.txt", "r"); //you can create HomeworkLib.txt file in that we give input
if (fp == NULL)
printf ("File doesn't exist ");
else
printKMostFreq (fp, k);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a program, called wordcount.c, that reads one word at a time from the standard input....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • Program Description: (C++). Write a word search program that searches an input data file for a...

    Program Description: (C++). Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of characters in the input data file that are not vowels. Your program must do this by providing the following functions : void processFile(ifstream &inFile, string &word, int &wordCount, int &nonVowelCount) ; void...

  • Creat a C Program that Reads words from a file called words, which contains one word...

    Creat a C Program that Reads words from a file called words, which contains one word per line.Each word has maximum length to M.The number of words in the file is equal to N. Incert each word to Binary Search Tree with node name dictionary.Each node of the tree must contain one word.The left child must contain a word that it is lexicographically smaller while the right child contains a lexicographically bigger one. 1) When you finish reading the file,...

  • Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public...

    Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public inorder function calls a private recursive function _ inorder to do the actual traversal. This public-private strategy is the correct way to implement recursive functions, where the public function kicks off the recursion and the private function does the actual work. The public function is written for you, your job is to implement the private _ inorder function. he main program has been written...

  • Using the program segment and sample txt file below, write a program that contains a linked...

    Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to: 1. initialize list of students 2. add additional student to front of list 3. add additional student to rear of list 4. delete student 5. sort students alphabetically 6. sort students by idNum 7. show number of students in list 8. print students 9. quit program XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The program should be divided into the...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • (in C) Binry Srch tree problem: 1. Need to read words from a “in” text file...

    (in C) Binry Srch tree problem: 1. Need to read words from a “in” text file and build a binary search tree. (1st line will state number of elements) 2. strcmp() can be used to compare data and decide were to insert a new node. 3. The output should include: -print the tree as in-order traversal. -print the character count in the tree. -Ask user input for a word to search, print an output if either found or not. (typing...

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • Write a program that opens a text file called input.txt and reads its contents into a...

    Write a program that opens a text file called input.txt and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file called output.txt. The order of the characters saved in the second file should be the reverse of their order in the first file. We don’t know the text file length. Input.txt This is a file is for test output.txt tset rof si elif...

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT