Question
Develop a C program which reads and processes an input text file like this textdata.txt. It retrieves information: number of lines, number of all words,
Develop a C program which reads and processes an input text file like this textdata.txt. It retrieves information:
- number of lines,
- number of all words,
- number of distinct non-common words,
- non-common words and their frequencies.
Note that common words, are also known as stop words, like words in this file).
It outputs the retrieved information to a file like this result.txt.
Specifically, write C source programs myword.h and myword.c containing the following:
- structure definitions:
typedef struct word { char word[30]; int frequency; } WORD; typedef struct words { WORD word_array[1000]; int line_count; int word_count; int keyword_count; } WORDSUMMARY;
-
void set_stopword(char *filename, char *stopwords[]); this functions reads stop words from the common word file by filename, and puts them in the stop word dictionary data structure as an array of 26 strings, each string holds all stop words starting with the same alphabet, separated by comma ,. The array of strings is passed parameter char *stopwords[], stopwords[i] holds the pointer of the i-th string.
-
int contain_word(char *stopwords[], char *word); this function checks if the given word is contained in stop word dictionary char stopwords[], it returns 1 if true otherwise 0.
-
int str_contain_word(char *str, char *word); this checks if the given *word is contained in a given string str, returns 1 if yes and 0 otherwise. For example, if str is the,this,that, word is this, then it returns 1.
-
int process_word(char *filename, WORDSUMMARY *words, char *stopwords[]); this function opens and reads text file of name passed by *filename line by line. For each line, it gets each word, if it is not a stop word, check if it is already in array words->word_array, if yes, increases its frequency by 1, otherwise inserts it to the end of the word_array and set its frequency 1. Meantime, it updates the count infomation.
-
int save_to_file(char *filename, WORDSUMMARY *words); this saves the data of WORDSUMMARY words to file of name passed by *filename in specified format.
-
Use the provided main function file a4q2.c to test your programs as the following.
Question 2
Copy of your mystring.h and mystring.c of a3q3 to a4 folder. If dont finish a3q3, refer to these mystring.h and mystring.c.
Save this main function program a4q2.c to a4 folder.
Create myword.h and then develop myword.c
myword.h
#include "mystring.h" #define MAX_WORD 30 #define MAX_LINE_LEN 1000 #define MAX_WORDS 1000 typedef struct word { char word[MAX_WORD]; int frequency; } WORD; typedef struct words { WORD word_array[MAX_WORDS]; int line_count; int word_count; int keyword_count; } WORDSUMMARY; void set_stopword(char *filename, char *stopwordsp[]); int contain_word(char *stopwords[], char *word); int str_contain_word(char *str, char *word); int process_word(char *filename, WORDSUMMARY *words, char *stopwords[]); int save_to_file(char *filename, WORDSUMMARY *words);
myword.c
#include "myword.h" void set_stopword(char *filename, char *stopwords[]) { // your implementation, refer to class code example 22 -- csv file read with string value } // this function check if the word is contained in directory stopwords[] // returns 1 if yes, 0 otherwise. It use function str_contain_word() int contain_word(char *stopwords[], char *word) { // your code } // this function check if word is a word in string str, // returns 1 if yes, 0 otherwise int str_contain_word(char *str, char *word) { if (str == NULL || word == NULL) return 0; // your code return 0; } int process_word(char *filename, WORDSUMMARY *words, char *stopwords[]) { const char delimiters[] = " .,;:!()&?- \t \"\'"; // your implementation } int save_to_file(char *filename, WORDSUMMARY *words) { // your implementation, use the following formats // fprintf(??, "%-20s %8d ", "Line count", words->line_count); // fprintf(??, "%-18s %10s ", "Keyword", "frequency"); return 0; }
- Compile and test
Save common-english-words.txt and textdata.txt to the a4 folder.
gcc mysting.c myword.c a4q2.c -o a4q2 a4q2 textdata.txt result.txt common-english-words.txt
build by make & makefile
Save this makefile as file name makefile in the a4 directory. Make sure that the space before each command is a tab.
Then use command
make
to compile, which will create files a4q2.o mystring.o myword.o a4q2.exe.
Use the following command to remove the object files a4q2.o mystring.o myword.o.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started