Saturday 25 April 2015

C Program to count number of occurance of a given character in a file

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.

  1. #include<stdio.h>
  2. int main() {
  3. int count=0;
  4. char sf[20];char rr;FILE *fp;char c;
  5. printf("Enter the file name :\n");
  6. gets(sf);
  7. printf("Enter the character to be counted :\n");
  8. scanf("%c",&rr);
  9. fp = fopen(sf, "r");
  10. while( ( c = fgetc(fp) ) != EOF )
  11. {
  12. if(c==rr)
  13. count++;
  14. }
  15. printf("File '%s' has %d instances of letter '%c'.",sf,count,rr);
  16. fclose(fp);
  17. return 0;
  18. }

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel