I am trying to learn the language C. Here is one thing I want to know. What are the program codes to compare and count character and line of a text file? Please send your valuable opinions.
- Login or Signup Now to post comments
I am trying to learn the language C. Here is one thing I want to know. What are the program codes to compare and count character and line of a text file? Please send your valuable opinions.
Hello Levi Jeensen ,
Comparing and counting character and line of a text file
The program codes are below
#include<stdio.h>
#include<string.h>
int main()
{
FILE *ptr_file;
char buf[200];
char key[] = "test";
int wordcount = 0;
ptr_file = fopen("input.txt","r");
while (fgets(buf,200, ptr_file)!=NULL)
{
if((strstr(buf,key)) !=NULL){
wordcount++;
}
}
fclose(ptr_file);
printf("%d",wordcount);
}
Thanks.