#include<stdio.h>
#include<string.h>
int main()
{
char str[50];
printf(“Enter the name of file : “);
scanf(“%s”, str);
// open file in read mode
FILE *ptr = fopen(str, “r”);
printf(“nContents of %s …nn”, str);
// store the number of times character has appeared in the string
// index 0 is fo a
// index 1 is fo b
// index 2 is fo c
// .
// .
// .
// index 25 is for z
int arr[26];
int i;
// set the count of each character to 0
for( i = 0 ; i < 26 ; i++ )
arr[i] = 0;
// read a character in a file
char ch = fgetc(ptr);
// loop untill file ends
while( ch != EOF )
{
// read a character in a file
ch = fgetc(ptr);
// get the ascii code of current character
// tolower() converts the character passed as argument in lower case
int ascii = (int)tolower(ch);
if( ascii >= 97 && ascii <= 122 )
{
// get the index of character in arr
int index = ascii – 97;
// increase the frequency of current character
arr[index]++;
}
}
// close the file
fclose(ptr);
for( i = 0 ; i < 26 ; i++ )
printf(“Letter %c or %c appears %d timesn”, (char)( i + 97 ), (char)( i + 65 ) , arr[i]);
return 0;
}
————————test.txt———————–
This is a list of courses.
CSC 1010 – COMPUTERS & APPLICATIONS
Sample Output
