Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C program called prog5.c that looks for words in a dictionary which hav

ID: 3740669 • Letter: C

Question

Create a C program called prog5.c that looks for words in a
dictionary which have certain properties as described below. You should
exhibit a proficiency in:
• Operations on Strings
• Dynamic Memory Allocation
• File Operations
• Using Command Line Arguments
• Calculating and Storing Statistics

The program should assume the standard “QWERTY” keyboard.

Input:

Your program is to input two command line arguments: the first is the path and name of
the dictionary file, dictionary, and the second is the path and name of the
output file, output_file, as given below.

prog5 dictionary output_file

Your program is to read in the contents of the dictionary provided into a dynamically
allocated array of pointers to strings,

e.g. char *Words[NUM_WORDS]

so that the array requires the minimum amount of storage for each word.

Your program should examine those words and find which words have the following
properties:
1. The longest word(s) typed only with the right hand.
2. The longest word(s) typed only with the left hand.
3. The longest word(s) typed with only the first letter row of the keyboard.
4. The longest word(s) typed with only the second letter row of the keyboard.
5. The longest word(s) typed with only the third letter row of the keyboard.
6. All words and longest word ending in ‘dous.’
7. All palindromes. (A palindrome is a word which is the same read
backwards. e.g. ‘kayak’)
8. All words and longest word having all five vowels in order.
9. The percentage of time the left hand is used to type all dictionary words in
order.

Output:

Your program should output the results of the searches listed above to both the screen,
and the output file, output_file.

Further Considerations:

Your program should be structured neatly, easily readable, and well commented.
Furthermore, variable and function names should be such that the software is as
“self-commenting” as possible.

Use the following line to compile your program:

gcc -Wall -g prog5.c -o prog5

I really need help with the code for 1-9 in the bold text above.

The code I have for this program so far is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

//Get line number function
int GetLineNum(char * filename) {
FILE * fp;
char c;
int NumofLine=0;
int size = 0;
int i = 0;
char ** array;
//Open the file
fp = fopen(filename,"r");
//check if file exists
if (fp == NULL) {
printf("Could not open file %s", filename);
return 0;
}
//Extract characters from file and store in character c
for (c = getc(fp); c != EOF ; c = getc(fp)) {
size++;
if (c == ' ') {
NumofLine++;
}
}
array = (char **) malloc(NumofLine*sizeof(char *));
for(i = 0 ; i<NumofLine; i++) {
array[i] = (char *) malloc(size*sizeof(char));
}
rewind(fp);
i = 0;
while(fgets(array[i],size,fp) != NULL) {
printf("%s",array[i++]);
//close file
fclose(fp);
for(i=0; i<NumofLine; i++) {
free(array[i]);
free(array);
}
}
return NumofLine;
}
int main(int argc, char *argv[]) {
int NumofLines;
NumofLines = GetLineNum(argv[1]);
printf("%d ", NumofLines);
return 0;
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

//Get line number function
int GetLineNum(char * filename) {
FILE * fp;
char c;
int NumofLine=0;
int size = 0;
int i = 0;
char ** array;
//Open the file
fp = fopen(filename,"r");
//check if file exists
if (fp == NULL) {
printf("Could not open file %s", filename);
return 0;
}
//Extract characters from file and store in character c
for (c = getc(fp); c != EOF ; c = getc(fp)) {
size++;
if (c == ' ') {
NumofLine++;
}
}
array = (char **) malloc(NumofLine*sizeof(char *));
for(i = 0 ; i<NumofLine; i++) {
array[i] = (char *) malloc(size*sizeof(char));
}
rewind(fp);
i = 0;
while(fgets(array[i],size,fp) != NULL) {
printf("%s",array[i++]);
//close file
fclose(fp);
for(i=0; i<NumofLine; i++) {
free(array[i]);
free(array);
}
}
return NumofLine;
}
int main(int argc, char *argv[]) {
int NumofLines;
NumofLines = GetLineNum(argv[1]);
printf("%d ", NumofLines);
return 0;
}