C Count Words in a File Program


Counting the number of words in a file is a common programming task that demonstrates file handling and string manipulation in C. Below is a simple C program that reads a text file and counts the number of words it contains.

Program: Count Words in a File in C

#include <stdio.h> #include <stdlib.h> #include <ctype.h> int main() { FILE *file; char ch; int inWord = 0; // Flag to indicate if we are inside a word int wordCount = 0; // Word count // Open the file in read mode file = fopen("input.txt", "r"); if (file == NULL) { perror("Error opening file"); return EXIT_FAILURE; } // Read characters from the file until EOF while ((ch = fgetc(file)) != EOF) { // Check if the character is a letter or digit if (isalpha(ch) || isdigit(ch)) { // If we are not already in a word, it means we found a new word if (!inWord) { inWord = 1; // Set the flag to indicate we are inside a word wordCount++; // Increment the word count } } else { // If we encounter a space or punctuation, we are no longer in a word inWord = 0; } } printf("Total number of words: %d\n", wordCount); // Close the file fclose(file); return EXIT_SUCCESS; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: For standard I/O functions.
    • #include <stdlib.h>: For exit status and error handling.
    • #include <ctype.h>: For character type functions like isalpha() and isdigit().
  2. File Pointer:

    • FILE *file;: Declares a pointer to handle the file.
  3. Variables:

    • char ch;: To store each character read from the file.
    • int inWord = 0;: A flag to track whether the program is currently inside a word.
    • int wordCount = 0;: Counter to keep track of the number of words.
  4. Open the File:

    • The program attempts to open input.txt in read mode:
      file = fopen("input.txt", "r");
    • If the file cannot be opened, it prints an error message and exits.
  5. Read Characters:

    • The program reads characters from the file in a loop until it reaches the end-of-file (EOF):
      while ((ch = fgetc(file)) != EOF) {
  6. Count Words:

    • Inside the loop, it checks if the character is a letter or digit using isalpha() and isdigit():
      if (isalpha(ch) || isdigit(ch)) {
      • If inWord is 0, it means the program has found a new word. It sets inWord to 1 and increments the wordCount:
      if (!inWord) { inWord = 1; wordCount++; }
    • If the character is not a letter or digit, the program sets inWord to 0, indicating that it has exited a word.
  7. Print Word Count:

    • After processing the file, the program prints the total number of words:
      printf("Total number of words: %d\n", wordCount);
  8. Close the File:

    • Finally, it closes the file to release resources:
      fclose(file);

How to Run the Program

  1. Create the Input File: Create a text file named input.txt in the same directory as the program and add some text to it.

  2. Compile the Code: Use a C compiler like gcc to compile the code:

    gcc count_words.c -o count_words
  3. Execute the Program: Run the compiled program:

    ./count_words

Example Output

Assuming input.txt contains the following text:

Hello, World! This is a test file. It contains several words.

After running the program, the output will be:

Total number of words: 9

Conclusion

This program effectively counts the number of words in a file by reading it character by character. It uses simple logic to identify words based on the presence of letters and digits, while treating spaces and punctuation as delimiters. This approach can be extended or modified for more complex word counting scenarios, such as handling hyphenated words, contractions, or specific punctuation rules.