I'm trying to write a program that takes a file and a string by using Standard C functions, the program counts all the characters in the file which the string contains.
For example if the user writes:
counter.exe x.txt abcd
The program calculates the number of each character that the string contains: a, b ,c ,d in file x.txt
Sample message:
Number of 'a' characters in 'x.txt' file is: 12
Number of 'b' characters in 'x.txt' file is: 0
Number of 'c' characters in 'x.txt' file is: 3
Number of 'd' characters in 'x.txt' file is: 5
So far I've been able to make it print and count one character from the file, how do I make it count all the characters that I tell it to count, not just the first one from the string?
counter.c code:
#include<stdio.h>
int main() {
int count = 0;
char sf[20]; char rr; FILE* fp; char c;
printf("Enter the file name :\n");
gets(sf);
printf("Enter the character to be counted :\n");
scanf("%c", &rr);
fp = fopen(sf, "r");
while ((c = fgetc(fp)) != EOF)
{
if (c == rr)
count++;
}
printf("File '%s' has %d instances of letter '%c'.", sf, count, rr);
fclose(fp);
return 0;
}