Question
Develop a C program which reads and processes an input text file like this textdata.txt. It retrieves the following information. number of lines number of
Develop a C program which reads and processes an input text file like this textdata.txt. It retrieves the following information.
- number of lines
- number of words
- number of distinct words
- words and their frequencies
The program outputs the retrieved information to a file like this result.txt.
Specifically, write C programs myword.h containing the following structure definitions and function headers, and myword.c containing the implementations of the functions. It is required to use your trim() and lower_case() functions from Q1, and it is allowed to use other string functions such as strcat() and strstr() from string library. string.h:
#ifndef SRC_MYWORD_H_ #define SRC_MYWORD_H_
#define MAX_WORD 30 #define MAX_LINE_LEN 1000 #define MAX_WORDS 1000
typedef struct word { char word[30]; int frequency; } WORD;
typedef struct words { WORD word_array[1000]; int line_count; int word_count; int distinct_word_count; } WORDINFO;
int process_word(char *filename, WORDINFO *wordinfo); int save_to_file(char *filename, WORDINFO *wordinfo);
#endif /* SRC_MYWORD_H_ */
Use the provided main function file myword_main.c to test your programs as the following.
Public test
gcc mystring.c myword.c myword_main.c -o q2 q2 word processing:done line count:2 word count:10 distinct word count:6 this:2 is:2 the:2 first:1 test:2 second:1 saving result to file:done
myword.c:
#include#include #include "mystring.h" #include "myword.h"
int process_word(char *infilename, WORDINFO *wip) { const char delimiters[] = " .,;:!()&?- \t \"\'"; // your implementation }
int save_to_file(char *outfilename, WORDINFO *wip) { // your implementation }
myword_main.c:
#include #include #include "mystring.h" #include "myword.h"
void display_word_summary(WORDINFO *wip) { printf("%s:%d ", "line count", wip->line_count); printf("%s:%d ", "word count", wip->word_count); printf("%s:%d ", "distinct word count", wip->distinct_word_count); int i; for (i = 0; i < wip->distinct_word_count; i++) { printf("%s:%d ", wip->word_array[i].word, wip->word_array[i].frequency); } }
int main(int argc, char *args[]) { char infilename[40] = "textdata.txt"; //default input file name char outfilename[40] = "result.txt"; //default output file name
if (argc > 1) { if (argc >= 2) strcpy(infilename, args[1]); if (argc >= 3) strcpy(outfilename, args[2]); }
WORDINFO wordinfo = { 0 }; process_word(infilename, &wordinfo); printf("word processing:done "); display_word_summary(&wordinfo); save_to_file(outfilename, &wordinfo); printf("saving result to file:done ");
return 0; }
result.txt:
line count:2 word count:10 distinct word count:6 this:2 is:2 the:2 first:1 test:2 second:1
textdata.txt:
THIS IS THE FIRST TEST. This is the second test.
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