Following is the program to count the occurrence of a given character in a file.
It takes two input i.e. the character that has to be counted, the file name and it gives the count of occurrence of the character in that file.
It takes two input i.e. the character that has to be counted, the file name and it gives the count of occurrence of the character in that file.
#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; }
0 comments:
Post a Comment