Write a C program named as getMostFreq.c that takes input text file as the most
ID: 3778905 • Letter: W
Question
Write a C program named as getMostFreq.c that takes input text file as the most frequent letter (ignoring cases) and displays how many times that the text file does not exist, print out an error message to the terminal For outputs could be like below $cat test.txt This is a list of courses. CSC 1010 - COMPUTERS & APPLICATIONS $./countC test.txt Most frequent letter is 's', it appeared 8 times $./countC NotExist.txt Error: file not exist. Question: Put the source code of getMostFreq.c in your answer sheet.Explanation / Answer
#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 frequency[26];
int ch = 0;
if (argc > 1)
inputFile = fopen(argv[1], "r");
else {
fprintf(stderr, "Error, file not exist ");
exit(EXIT_FAILURE);
}
if (inputFile == NULL) {
fprintf(stderr, "Error, file not exist ");
exit(EXIT_FAILURE);
}
else {
for (ch = 0; ch < 26; ch++)
frequency[ch] = 0;
while(1){
ch = fgetc(fileHandle);
if (ch == EOF) break;
if ('a' <= ch && ch <= 'z')
frequency[ch - 'a']++;
else if ('A' <= ch && ch <= 'Z')
frequency[ch - 'A']++;
}
int maxCount = 0;
int maxChar = 0;
// i = A to Z
for (int i = 0; i <= 26; ++i)
{
// if freq of this char is greater than the previous max freq
if (frequency[i] > maxCount)
{
// store the value of the max freq
maxCount = frequency[i];
// store the char that had the max freq
maxChar = i;
}
}
fprintf("Most frequent letter is %c and it appears %d",maxChar,maxCount);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.