Javascript required
Skip to content Skip to sidebar Skip to footer

C++ How to Read the Numbers 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 treatment plan in c. Therefore, we volition use some file treatment functions in this program.

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

Earlier writing the plan, we must know how to identify the words. After some alphabet, if at that place is a space or a new line, then we can say this is a word. Nosotros have to use the same logic in this plan.

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

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

Hello, dear friends. How are you?

Let u.s.a. come across the expected output starting time.

Output for Counting Number of Words in a Text File

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

As you can run into from the to a higher place output, we are counting the total number of words on the basis of bare spaces. In other words, we can say, we are counting the total number of spaces plus 1.

Why plus ane? The reason is nosotros are likewise counting the EOF which is the end of the file. Now, let united states of america meet the bodily c programme for counting the full number of words.

Too Read: C Program to Remove Zeros from a number

C Programme 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 -one;     }     printf("Contents of the file are\n\n");     while((ch=fgetc(fp))!=EOF)     {         putchar(ch);         if((ch==' ')||(ch=='\n'))         {             words++;         }     }     fclose(fp);     printf("\north\nTotal Number of Words = %d",words+1);     return 0; }

Caption

Now, we volition run into the explanation in short.

Variable declarations

Here, we take declared three variables. Ane 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. And then, we will compare the value of this ch variable with bare infinite and new line grapheme i.e. \northward.

The variable "words" which is of int type volition store the number of words.

Likewise Read: The while loop in C Programming

While loop

In the while loop, we are doing the post-obit things.

  1. We are reading the single grapheme 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 in that location is no EOF, so 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, information technology is time to count the number of words. For this, we have written 1 if statment and comparison 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.

Terminal 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 programme. If you lot know the Hindi language, then you lot can watch the following video.

Cheers.

Some Important C Programs

  1. Plan 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 Alternating Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circumvolve 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 role in c
  10. The while loop in C Programming

C++ How to Read the Numbers of Words in a Text File

Source: https://hplusacademy.com/c-program-to-count-total-number-of-words-in-a-text-file/