C Programming Question In this project, you are asked to write a program which c
ID: 3828028 • Letter: C
Question
C Programming Question
In this project, you are asked to write a program which can open a text file, read its content and count the number of each alphabetic character regardless of its case. It means that program needs to count for example how many letter of 'A' or 'a' are in the text file. The counting is for all alphabetic characters; thus, all other characters such as numbers and punctuation characters should be excluded. The output of the program should be formatted in two columns as: A # B # C # .... z #Explanation / Answer
Thsi code assume that file containing text is input.txt, you can change it by changing code in bold below.
#include <stdio.h>
#include <ctype.h>
int main()
{
int arr[26];
int i;
for(i = 0 ; i < 26; i++)
arr[i] = 0;
FILE *fp = fopen("input.txt", "r");
char ch;
while((ch = getc(fp)) != EOF)
{
if (isalpha(ch))
{
if(islower(ch))
{
arr[ch -'a']++;
}
else
{
arr[ch - 'A']++;
}
}
}
for(i = 0 ; i < 26; i++)
{
printf("%c %d ", 'A' + i, arr[i]);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.