How to Write a C Program to Count Lines, Characters, and Words Using for-Loop Only

How to Write a C Program to Count Lines, Characters, and Words Using for-Loop Only

Counting lines, characters, and words in a text file is a common task in programming. This guide will walk you through the process of writing a C program that accomplishes this task using a for-loop. The following steps and code will ensure that you can achieve your goal efficiently.

Step-by-Step Guide

To write a program in C that counts the number of lines, characters, and words in a text file using only a for-loop, follow the steps outlined below:

Open the file for reading: Use the FILE data type and FILE *file to declare and open the file for reading. Read each character of the file one by one: Use while or for loops along with the fgetc(file) function to read each character until the end of the file (EOF). Count characters, words, and lines based on the characters read: Implement logic to increment the respective counters as characters are read. Close the file and display the counts: Use fclose(file) to close the file and printf to display the counts.

Sample Code

The following code demonstrates how to implement the above steps:

include stdio.hinclude ctype.hint main() {    FILE *file;    char ch;    int lines  0, words  0, characters  0;    int inWord  0; // Flag to track if we are in a word    // Open the file in read mode    file  fopen("example.txt", "r");    if (file  NULL) {        printf("Unable to open file
");        return 1;    }    // Read each character from the file    while (ch ! EOF) {        characters  ; // Count characters        // Check for new line        if (ch  '
') {            lines  ; // Increment line count        }        // Check for word boundaries        if (isspace(ch)) {            if (inWord) {                words  ; // Increment word count when we exit a word                inWord  0; // Reset the word flag            }        } else {            inWord  1; // We are inside a word        }        ch  fgetc(file);    }    // If the last character was not a newline, count the last word    if (inWord) {        words  ;    }    // Close the file    fclose(file);    // Print the results    printf("Number of lines: %d
", lines);    printf("Number of characters: %d
", characters);    printf("Number of words: %d
", words);    return 0;}

Explanation

In the provided code:

File Handling: The program opens a file named example.txt for reading. Make sure to replace example.txt with the path to your desired text file. Character Reading: The fgetc(file) function reads one character at a time until the end of the file (EOF) is reached. Counting Logic: Characters: Each character read increments the characters count. Lines: A newline character ' ' increments the lines count. Words: The program uses a flag inWord to track whether it is currently inside a word. Words are counted when transitioning from non-word characters (whitespace or newline) to word characters. Final Word Count: After the loop, if the last character read was part of a word, it is counted as a word.

Compilation and Execution

To compile and run the program, use the following commands in your terminal:

gcc -o word_counter word_counter.c./word_counter

Make sure you have a text file named example.txt in the same directory as your compiled program or adjust the filename in the code accordingly.

Related Keywords and SEO

The following are the relevant keywords for this content which can be used to improve the SEO:

C programming for-loop text file

Note: For detailed optimization, consider integrating these keywords into your meta tags, headers, and content throughout the page.