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

Need help please. Thank you Write a C program named as getMostFreq.c that takes

ID: 3571660 • Letter: N

Question


Need help please. Thank you Write a C program named as getMostFreq.c that takes input text file as arguments, outputs the most frequent letter (ignoring cases) and displays how many times that letter appears. If the text file does not exist, print out an error message to the terminal. For example, sample outputs could be like below Put the source code of getMostFreq.c in your answer sheet. In your answer sheet, please also attach screenshots of the outputs using the test.txt file in the example. Upload your file getMostFreq.c to iCollege.

Explanation / Answer

//This program will tell you the occcurance of each digit and letter and will tell you the most frequent letter and also digit in file.


#include <stdio.h>

#include <sys/types.h>

#include <fcntl.h>

#include <sys/stat.h>

#include <unistd.h>

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

#include <ctype.h>

int main (int argc, char *argv[]) {

FILE *inputFile;

char str[1000];

int mdigits[10];

char mletters[26]; /* declared as char; tried declaring as int as well */

if (argc > 1)

inputFile = fopen(argv[1], "r");

else {

fprintf(stderr, "Error, file name not provided in the command line ");

exit(EXIT_FAILURE);
}

if (inputFile == NULL) {

fprintf(stderr, "Error, file not found ");

exit(EXIT_FAILURE);

}

else {


int i, u;

for (i = 0; i < 10; i++)

mdigits[i] = 0;

for (u = 0; u < 26; u++)

mletters[u] = 0;

int length, ch;

for(length = 0; (ch = fgetc(inputFile)) != EOF && (length + 1) < 1000; length++) {

str[length] = ch;

if (ch >= '0' && ch <= '9')

++mdigits[ch - '0'];

else if (ch >= 'a' && ch <= 'Z') { /* problem is here obviously */

if (ch >= 'A' && ch <= 'Z')

ch = tolower(ch); /* have to increment on each occurrence of letter */

++mletters[ch]; /* here! is this legal with a char array? */ /* how can increment the value for each letter */

}
}

str[length] = '';

fclose(inputFile);

}

int pid;

pid = fork();

if (pid == 0) { /* CHILD PROCESS */

FILE *outputFile;

outputFile = fopen(argv[1], "a+");

if (outputFile == NULL) {

fprintf(stderr, "Error, cannot write to the file assign6.out ");

exit(EXIT_FAILURE);

}

else { /* PARENT PROCESS */

fprintf(outputFile, " ");

/* appends to file */

fprintf(outputFile, "The most frequent digit is %d "

"The most frequent letter is %c%c ", mostfrequent(mdigits, 10),

toupper(mostfrequent2(mletters, 26)), mostfrequent2(mletters, 26));

}

fclose(outputFile);

}

else {

int i;

for(i = 0; i < 10; i++)

printf("The number of '%d' is %d ", i, mdigits[i]);

/* prints the frequency of each letter */

int u;

char x, y;

for (u = 0, x = 'A', y = 'a'; u < 26 && x <= 'Z' && y <= 'z'; u++, x++, y++)

printf("The number of '%c' and '%c' is %d ", x, y, mletters[u]);

}

}

int mostfrequent(int *a, int size) {

int index = 0;

int max = a[0];

int i;

for (i = 1; i < size; i++)

if (a[i] > max) {

max = a[i];

index = i;

}

return index;

}

/* not sure if this is doing its intended purpose, works with int if var max is int */

int mostfrequent2(char *a, int size) {

int index = 0;

char max = a[0];

int i;

for (i = 1; i < size; i++)
if (a[i] > max) {

max = a[i];

index = i;

}

return index;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote