C Program to Count Total Number of Words in a Text File

Introduction

In this post, I am going to write a c program to count the total number of words in a text file. This is a file handling program in c. Therefore, we will use some file handling functions in this program.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

Before writing the program, we must know how to identify the words. After some alphabet, if there is a space or a new line, then we can say this is a word. We have to use the same logic in this program.

Also Read: C Program to Print Multiples of 5 using do-while loop

Here is the input file “abc.txt” that we are going to read.

Hello, dear friends.
How are you?

Let us see the expected output first.

Output for Counting Number of Words in a Text File

Fig. Output – C Program to Count Total Number of Words in a Text File

As you can see from the above output, we are counting the total number of words on the basis of blank spaces. In other words, we can say, we are counting the total number of spaces plus 1.

Why plus 1? The reason is we are also counting the EOF which is the end of the file. Now, let us see the actual c program for counting the total number of words.

Also Read: C Program to Remove Zeros from a number

C Program to Count Total Number of Words in a Text File

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fp;
    char ch;
    int words=0;
    fp=fopen("abc.txt","r");
    if(fp==NULL)
    {
        printf("There is an error while opening a file");
        return -1;
    }
    printf("Contents of the file are\n\n");
    while((ch=fgetc(fp))!=EOF)
    {
        putchar(ch);
        if((ch==' ')||(ch=='\n'))
        {
            words++;
        }
    }
    fclose(fp);
    printf("\n\nTotal Number of Words = %d",words+1);
    return 0;
}

Explanation

Now, we will see the explanation in short.

Variable declarations

Here, we have declared three variables. One is of FILE type, one is of char type and the other is of type int.

The “fp” is the file pointer that is associated with the opened file.

The variable “ch” will store the character that is read from the file. Then, we will compare the value of this ch variable with blank space and new line character i.e. \n.

The variable “words” which is of int type will store the number of words.

Also Read: The while loop in C Programming

While loop

In the while loop, we are doing the following things.

  1. We are reading the single character from the file that is associated with the file pointer fp and storing that character in the variable ch.
  2. Then we are comparing the value of ch and EOF i.e. end of the file.
  3. If there is no EOF, then we will execute the body of while loop.
  4. In the while loop, we are going to display the contents of the file using putchar() function.
  5. Now, it is time to count the number of words. For this, we have written one if statment and comparing the value of ch with blank space and new line character \n with logical OR operator. If any of these condition get true, then we will increment the value of the variable words.

Final Output

In the end, using the printf() function, we will display the total number of words in a file abc.txt which is a text file.

I hope you have understood this program. If you know the Hindi language, then you can watch the following video.

Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

Leave a Comment