Question
A C program needs to be run on ocelot to read a text file and print it to the display. It should optionally find the
A C program needs to be run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring. You must use getopt to parse the command line. There is no user input while this program is running.
Usage: mywords [-c] [-f substring] filename
The -c flag means to count the number of words in the file.
A word would be a series of characters separated by spaces or punctuation. A word could include a hyphen or a single apostrophe.
The -f option will find the number of occurrences of the given substring.
You may have any number of the flags included or none of them.
The order they should be run would be: -c first and -f second.
Code needs to be ran with:
./mywords -c f he testfile
A makefile needs to be created to compile the program to create executable mywords. An ascii file name testfile with the text: The boy is sitting right there.
Don't copy or paste any of the solutions in this site; they're talking about a different variant of this question. I need some help here. Thanks.
Current code:
#include
#include
#include
//Declare constants
#define n 10000
#define OUT 0
#define IN 1
//Implement method countWords()
unsigned countWords(char *str)
{
//declare local variables
int state = OUT;
unsigned wc = 0;
while (*str)
{
if (*str == ' ' || *str == ' ' || *str == ' ')
{
state = OUT;
}
else if (state == OUT)
{
state = IN;
++wc;
}
++str;
}
return wc;
}
//Implement method all_words
void all_words(char *str1, int num_words)
{
//Declare local variables
int i, j, c = 0;
char temp[50], *split_str, str[60][20];
split_str = strtok(str1, " ,.-");
//use while loop to read the string until null
while (split_str != NULL)
{
strcpy(str[c], split_str);
split_str = strtok(NULL, " ,.-");
c++;
}
for (i = 1; i { for (j = 1; j <= num_words - i; j++) { if (strcmp(str[j - 1], str[j])>0) { strcpy(temp, str[j - 1]); strcpy(str[j - 1], str[j]); strcpy(str[j], temp); } } } } //Implement method to find occurence of string void search_occ(char *word, char *all_str) { //Declare local variables int v = 0, m = 0, times = 0, len; len = strlen(word); while (all_str[v] != ' ') { if (all_str[v] == word[m]) { while (all_str[v] == word[m] && all_str[v] != ' ') { v++; m++; } if (m == len && (all_str[n] == ' ' || all_str[v] == ' ')) { times++; } } else { while (all_str[v] != ' ') { v++; if (all_str[v] == ' ') break; } } v++; m = 0; } //Print the occurence of times if (times > 0) { printf(" '%s' appears %d time(s) ", word, times); } else { printf(" '%s' does not appear in the sentence. ", word); } } //main method int main(int argc, char *argv[]) { int opt; //Declare local variables char buff[n], search_str[100]; FILE *file; size_t nread; int i, num_of_words; //open the file file = fopen(argv[5], "r"); //If file if (file) { //print the file content printf("File Content... "); while ((nread = fread(buff, 1, sizeof buff, file)) > 0) fwrite(buff, 1, nread, stdout); if (ferror(file)) { //print error printf("Error...."); } fclose(file); } //use while loop to read the parameters while ((opt = getopt(argc, argv, ":-f:cs")) != -1) { switch (opt) { //if command option is c //call the method to count number of words case 'c': num_of_words = countWords(buff); printf(" Number of words-->>%d ", num_of_words); break; //if command option is f //call the method to search occurence of words case 'f': printf("substring: %s ", optarg); search_occ(optarg, buff); break; case '?': printf("unknown option: %c ", optopt); break; } } //return 0 return 0; } I need some help with my code.
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