#include <stdio.h>
#include <stdlib.h>
void totalLetters(FILE *filePtr, int *count, int *allCount, char letter);
int main ()
{
char letter;
int counter;
int frequency;
float percent;
FILE *inFilePtr ;
inFilePtr = fopen("file1.txt", "r");
printf("Enter a letter:\n");
scanf("%c", &letter);
totalLetters(inFilePtr, &frequency, &counter, letter);
printf("The letter %c appeared %d times out of a total of %d letters. \n", letter, frequency, counter);
percent = frequency/counter;
printf("%f of the letters", percent);
fclose(inFilePtr);
return 0;
} /* end main */
/* count is a pointer to the count of the letter specified by the user
allCount is a pointer to the count of the total letters in the file
letter is the letter specified by the user */
void totalLetters(FILE *filePtr, int *count, int *allCount, char letter)
{
char c;
int counter=0;
int frequency=0;
float percent;
if (filePtr == 0)
printf("Could not open the file\n");
else {
fscanf(filePtr, "%c", &c);
while(!feof(filePtr)) {
if (tolower(c) == letter) {
frequency++; }
if ((c >= 65 && c <91) || (c >= 97 && c < 123)) { /* end if statement*/
counter++; }
fscanf(filePtr,"%c", &c);
} /* end while loop*/
} /* end if-else statement*/
*count = frequency;
*allCount = counter;
} /* end function totalLetters */
|