Explanation
The program counts the number of words in a sentence entered by the user. Words are separated by spaces, tabs, or newlines.
Header
The code begins with including the standard input-output header file and the string header file using #include <stdio.h>
and #include <string.h>
for string manipulation functions.
Variable Declaration
The main()
function is defined, and a character array d
and an integer variable j
are declared:
char d[100];
: Declares a character array to hold the user's sentence.int j = 0;
: Initializes a variable to count the number of words.int len;
: Declares a variable to store the length of the entered sentence.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter a sentence. The input is read using fgets(d, sizeof(d), stdin);
to include spaces:
fgets(d, sizeof(d), stdin);
: Reads the sentence including spaces and stores it in the character arrayd
.
Word Count Calculation
The program calculates the number of words in the entered sentence:
int len = strlen(d);
: Calculates the length of the entered sentence.for (int m = 0; m < len; m++)
: Loops through each character in the sentence.if (d[m] == ' ' || d[m] == '\t' || d[m] == '\n')
: Checks if the current character is a space, tab, or newline.j++;
: Increments the word count if a space, tab, or newline is found.
Output Result
The program prints the total number of words counted:
printf("Number of words: %d\n", j);
: Prints the number of words in the entered sentence.
Program End
The program ends with return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
int main()
{
char d[100];
printf("\n\n Program to count the number of words in the sentence entered by the user. Example: This is dmj.one and you are learning about a code made in C: Total Words = 14. \n\n");
printf("Enter a sentence: ");
fgets(d, sizeof(d), stdin); // Read the sentence, including spaces
// Count the number of words in the d
int j = 0;
int len = strlen(d);
for (int m = 0; m < len; m++)
{
if (d[m] == ' ' || d[m] == '\t' || d[m] == '\n')
{
// If the current character is a space, tab, or newline, increment the word count
j++;
}
}
// Print the result
printf("Number of words: %d\n", j);
return 0;
}
Output
Program to count the number of words in the sentence entered by the user. Example: This is dmj.one and you are learning about a code made in C: Total Words = 14.
Enter a sentence: This is a demo sentence.
Number of words: 5