File : Count Character in C Programming
Code:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main(void)
{
FILE *fptr=NULL;
char c[2],get;
int ch;
int count=0;
char filename[100];
printf("Enter the file name\n");
scanf("%19s",filename);
fptr=fopen(filename,"r");
printf("Enter the character to be counted\n");
scanf(" %s",c);
if(fptr==NULL)
{
exit(-1);
}
if(((int)c[0]>=65)&&((int)c[0]<=122))
{
if(((int)c[0]>=97)&&((int)c[0]<=122))
ch=(int)c[0]-32;
else
ch=(int)c[0]+32;
}
do{
get=fgetc(fptr);
if(((int)get==ch)||get==c[0])
count=count+1;
}while(get!=EOF);
printf("File '%s' has %d instances of letter '%s'.",filename,count,c);
fclose(fptr);
return 0;
}
No comments