How to count total words and characters in file in C

#include
#include
int character(FILE *ptr1);
int word(FILE *ptr2);
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

int main(int argc, char *argv[]) {
FILE *file;
file=fopen(“ali.txt”,”r”);
printf(“Total character is %d\n”,character(file));
printf(“Total words is %d”,word(file));

return 0;
}
int character(FILE *ptr1){
int count=0;

while(!feof(ptr1)){
char c=getc(ptr1);
if(!isspace(c)){
count++;
}
}
return count-1;
}
int word(FILE *ptr2){
int count=1;

while(!feof(ptr2)){
char c=getc(ptr2);
if(isspace(c)){
count++;
}
}
return count;
}

Leave a Reply

Your email address will not be published. Required fields are marked *