Write a C program named as getMostFreq.c that takes input text file as arguments
ID: 3576216 • Letter: W
Question
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 $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. 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.Explanation / Answer
// C code getMostFreq.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main(int argc, char const *argv[])
{
char string[100];
int count[26] = {0};
FILE *fptr;
if ((fptr = fopen(argv[1], "r")) == NULL)
{
printf("Error: File not found ");
return 0;
}
while(1)
{
int i = 0;
fscanf(fptr,"%s", string);
while (string[i] != '')
{
if (tolower(string[i]) >= 'a' && tolower(string[i]) <= 'z')
count[tolower(string[i])-'a']++;
i++;
}
if (feof(fptr))
{
break;
}
}
fclose(fptr);
int j;
int max = count[0];
int index;
for (j = 0; j < 26; j++)
{
if(count[j] > max)
{
max = count[j];
index = j;
}
}
printf("Most frequent letter is %c, it appeared %d times ",(index+'a'), count[index]);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.