Question: Write a C program to run on UNIX to find the total count of words and optionally the longest and or shortest words in a
Write a C program to run on UNIX to find the total count of words and optionally the longest and or shortest words in a string input by the user or coming from a file. If there is no filename the user would be prompted to enter the string. You must use getopt to parse the command line. The string would not be input on the command line.
Usage: countwords [-l] [-s] [filename]
The l flag means to find the longest word in the string. The s option means to find the shortest word in the string. You may have both or one of the flags. Output should be well formatted and easy to read. Code should be nicely indented and commented. Create a simple Makefile to compile your program into an executable called count words.
I cant figure out how to get the getopt working properly. This is my code that needs to be fixed:
#include
// Functions declared after main char* input(); char* file(char* input_file); char* countwords();
// Takes in arguments. Calls functions based on user input. int main(int argc, char **argv) { extern char *optarg; extern int optind; char *f = NULL; int c;
static char usage[] = "countwords [-l] [-s] [filename]";
// Receives command line arguments while ((c = getopt(argc, argv, "")) != -1) { switch (c) { case '?': { printf(usage); exit(1); } } }
// Gets the last command in the command line if (optind < argc) { for (; optind < argc; optind++) { f = argv[optind]; } }
// Determines which function to call/use if(f != NULL) { printf(file(f)); } else { printf(countwords()); }
// Successfully terminate program return 0; }
// Receives the information from a file and determines the min and max words in the file char* file(char *input_file) {
int scount = 500, lcount = 0, i = 0; char *max, *min, *string = malloc(1024); FILE *f;
// Determines if the file exists or not if(!(f = fopen(input_file, "r"))) { printf("File does not exist. Usage: countwords [-l] [-s] [filename]"); exit(1); }
// Reads until the end of file while (!feof(f)) { char temp[100]; int c = fgetc(f);
for (; c != ' ' && c!= EOF; c = fgetc(f)) { temp[i] = c; i++; if (c == ' ') { if (i < scount) { scount = i--; min = strdup(temp); } i = 0; memset(&temp[0], 0, sizeof(temp)); } else { if(i > lcount) { lcount = i; max = strdup(temp); } } } } // Prints the information to a string, closes the file from reading, then returns the string sprintf(string, " The largest word is: %s. The smallest word is: %s. ", max, min); fclose(f); return string; }
// Receives user input and determines the min and max words in the data char* input() { int scount = 500, lcount = 0, i = 0, num = 0, c; char *max, *min, *usage = "Enter a string: ", temp[1024], *string = malloc(1024); printf(usage); c = fgetc(stdin); // Reads until the user presses enter for (; c != ' ' && c != EOF; c = fgetc(stdin)) { temp[i] = c; i++; if (c == ' ') { if (i < scount) { scount = i--; min = strdup(temp); } i = 0; memset(&temp[0], 0, sizeof(temp)); } if(i > lcount) { lcount = i; max = strdup(temp); } } if (i < scount) { scount = i; min = strdup(temp); }
memset(&temp[0], 0, sizeof(temp)); // Prints the information to a string then returns the string. sprintf(string, " The largest word is: %s. The smallest word is: %s. ", max, min); return string;
}
//Determines the number of words in a given string char* countwords(){ int count = 0, i; char string[100]; printf("Enter a string: "); fgets(string,100,stdin); for(i=0; string[i]!='\0'; i++){ if(string[i] == ' ') count++; } printf("The count of words in the string is %d ", count+1);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
