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
- Include Libraries: The program includes standard input-output library
#include <stdio.h>
and string manipulation library#include <string.h>
. - Define the main Function: The main function is the entry point of the program where the program execution begins.
- 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.
- Read Input Text: The program prompts the user to enter the text and reads it using
fgets()
function. - 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.
- 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.
- 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 likeprintf
andfgets
.#include <string.h>
: This library is used for string handling functions likestrlen
.
Variable Declarations
char text[1000];
: Array to store the input text.int i, word_count = 0;
:i
is the loop counter, andword_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 thetext
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 thein_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.