C program to count lines, words and characters in a file

Last updated on 06th January 2016

This is a C program that counts the number of lines, words and characters in a text file. At first the program prompts the user to enter the name of the file to be checked. The filename is passed as a parameter to the C Library function, fopen(). The second parameter "r" of the fopen function specifies the file should be opened in read only mode. The fopen function returns a pointer to the FILE object if the file is opened successfully.

The file is then read one character at a time using the fgetc() library function inside a while loop until the End of File (EOF) is reached. Character count is incremented with each iteration of the loop, word count is incremented when a Space character is encountered and line count is incremented for each new line character that is read.

Finally, linecount and wordcount are incremented by one if the character count is more than zero. Without this step the final word and line won't be counted.

Program Source Code

/*************************************************
 * C program to count no of lines, words and 	 *
 * characters in a file.			 *
 *************************************************/

#include <stdio.h>

int main()
{
 FILE *fp;
 char filename[100];
 char ch;
 int linecount, wordcount, charcount;

 // Initialize counter variables
 linecount = 0;
 wordcount = 0;
 charcount = 0;

 // Prompt user to enter filename
  printf("Enter a filename :");
  gets(filename);
  
    // Open file in read-only mode
   fp = fopen(filename,"r");

   // If file opened successfully, then write the string to file
   if ( fp ) {
     //Repeat until End Of File character is reached.	
     while ((ch=getc(fp)) != EOF) {
       // Increment character count if NOT new line or space
        if (ch != ' ' && ch != '\n') { ++charcount; }
        // Increment word count if new line or space character
        if (ch == ' ' || ch == '\n') { ++wordcount; }
        // Increment line count if new line character
        if (ch == '\n') { ++linecount; }	   
     }

     if (charcount > 0) {
        ++linecount;
        ++wordcount;
     }
   }
   else {
         printf("Failed to open the file\n");
   }

    printf("Lines : %d \n", linecount);
    printf("Words : %d \n", wordcount);
    printf("Characters : %d \n", charcount);

getchar();
return(0);
}

Sample Output

Consider a text file named wolf.txt with the following content:

Help Help, 
Go away you naughty wolf.

With the above file as input, the program will be show the result as below


Enter a filename :wolf.txt
Lines : 2
Words : 7
Characters : 30

Also Read


Post a comment

Comments

Asgar | March 28, 2019 7:13 AM |

How to write sentence? Is this clarified. If yes then where.

PG | February 8, 2019 3:39 PM |

This doesn't work. Character count should include ALL characters, including newlines and spaces. Additionally, some files have multiple spaces between words (such as an indented paragraph). This program does not account for multiple spaces with regards to word count.

abhishek | August 15, 2018 8:28 AM |

I WANT THIS QUE ANS PLS... WAP to input the name of a text file from the user and display: a) number of blanks present in file. b) number of lines present in file. c) number of capital alphabets present in file. d) number of small alphabets present in file. e) number of lines starting with a capital alphabet. g) number of digits present in file. h) The number of words ending with a vowel.

Sambodhi Mukherjee | July 18, 2017 6:40 AM |

It helps me a lot....thank u very much...

smile | May 8, 2018 6:52 PM |

can you help me I don't understand it.

Pob | March 6, 2018 2:47 PM |

Why if condition used here

ranjan | September 1, 2017 3:18 PM |

what if we two or more spaces are given together between two words ex - i am going to kolkata

bala | June 17, 2017 1:51 PM |

what in case if we are many spaces between two words., it will keep on counting..??

Nada | February 26, 2017 10:07 AM |

The program fails to open the file i am entering. Can't understand why?? can someone help?

Mark | January 15, 2017 6:14 PM |

This was gr8 thx m8, used it in my little project 😏👌

amit | December 17, 2016 8:30 PM |

If two or more space put between two words the word counter variable many times increment..

ali | December 14, 2016 3:20 PM |

this is a great code..well done

aahmed asaad | December 7, 2016 6:51 AM |

this program gives a wrong output if there more one space between each word!!!!

prathmesh dudhnikar | November 19, 2016 2:27 AM |

Excellent program i like

adarsh | September 10, 2016 6:22 PM |

How to the to find the no of files ?

philips05 | July 25, 2016 10:59 PM |

Excellent approach to share these source lines on programming. I really impressed from this work.

Manya | April 25, 2016 9:34 PM |

i am very much impressed keep it up guys

Manya | April 25, 2016 9:34 PM |

i am very much impressed where were you guys all along. now i can implement this in my little project

Manya | April 25, 2016 9:34 PM |

very much impressed