Word Count Program in C

Program Explanation

This C program counts the number of words in a given text. It reads the input text from the user, processes the text, and counts the words. A word is defined as a sequence of characters separated by whitespace (spaces, tabs, or newlines).

Program Structure

  1. Include Libraries: The program includes standard input-output library #include <stdio.h> and string manipulation library #include <string.h>.
  2. Define the main Function: The main function is the entry point of the program where the program execution begins.
  3. Declare Variables: Variables are declared for storing the input text, the current character being processed, the count of words, and a flag to indicate whether the previous character was a whitespace.
  4. Read Input Text: The program prompts the user to enter the text and reads it using fgets() function.
  5. Process the Text: The program iterates through each character of the input text. If the current character is a whitespace and the previous character was not a whitespace, it increments the word count.
  6. Count the Last Word: If the last character processed is not a whitespace, the program increments the word count to account for the last word.
  7. Output the Word Count: The program prints the total number of words in the given text.

Program Code

#include <stdio.h>
#include <string.h>

int main() {
    char text[1000];
    int i, word_count = 0;
    int in_word = 0;

    printf("Enter the text: ");
    fgets(text, sizeof(text), stdin);

    for (i = 0; i < strlen(text); i++) {
        if (text[i] == ' ' || text[i] == '\n' || text[i] == '\t') {
            if (in_word) {
                word_count++;
                in_word = 0;
            }
        } else {
            in_word = 1;
        }
    }

    // If the last character is not a whitespace, count the last word
    if (in_word) {
        word_count++;
    }

    printf("Number of words: %d\n", word_count);

    return 0;
}

Program Documentation

Libraries Included

  • #include <stdio.h>: This library is used for standard input and output functions like printf and fgets.
  • #include <string.h>: This library is used for string handling functions like strlen.

Variable Declarations

  • char text[1000];: Array to store the input text.
  • int i, word_count = 0;: i is the loop counter, and word_count stores the number of words.
  • int in_word = 0;: Flag to check if the current character is part of a word.

Reading Input Text

  • fgets(text, sizeof(text), stdin);: Reads a line of text from the standard input and stores it in the text array.

Processing the Text

  • The for loop iterates through each character of the input text.
  • Checks if the current character is a whitespace (' ', '\n', or '\t').
  • If a whitespace is found and the previous character was part of a word, it increments the word_count and resets the in_word flag.
  • If a non-whitespace character is found, it sets the in_word flag.

Counting the Last Word

  • If the last character processed is not a whitespace, the program increments the word_count to account for the last word.

Outputting the Word Count

  • printf("Number of words: %d\n", word_count);: Prints the total number of words in the given text.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)