No of visitors who read this post: 177
Category: C
Type: Question
Author: Levi Jeensen
Your rating: None Average: 3 (1 vote)

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

#

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.