C Program to Count Total Number of Words in a String

Write a C Program to Count Total Number of Words in a String with example.

C Program to Count Total Number of Words in a String Example 1

This program allows the user to enter a string (or character array), and a character value. Next, it will count the total number of words present inside this string using For Loop.

/* C Program to Count Total Number of Words in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100];
  	int i, totalwords;
  	totalwords = 1;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	 	   	
  	for(i = 0; str[i] != '\0'; i++)
	{
		if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
		{
			totalwords++;	
		} 
	}	
	printf("\n The Total Number of Words in this String %s  = %d", str, totalwords);
	
  	return 0;
}
C Program to Count Total Number of Words in a String 1

Here, we used For Loop to iterate each and every character in a String, and find the empty spaces in it.

for(i = 0; str[i] != '\0'; i++)
{
	if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
	{
		totalwords++;	
	} 
}

str[] = Hello World

First For Loop – First Iteration: for(i = 0; str[i] != ‘\0’ ; i++)
The condition is True
if(str[i] == ‘ ‘ || str[i] == ‘\n’ || str[i] == ‘\t’)
=> if(H == ‘ ‘ || H == ‘\n’ || H == ‘\t’) – Condition is False

Do the same for remaining iteration. If the condition is True (once it reaches empty space after Hello) then the compiler will execute totalwords++;
In this case totalwords = 2 (because it is already 1)

At last, we used the C Programming printf statement to print the total number of words in this string

printf("\n The Total Number of Words in this String %s  = %d", str, totalwords);

Program to Count Total Number of Words in a String Example 2

This C program for total string words is the same as above. Here, we just replaced the For Loop with While Loop. I suggest you refer to While Loop to understand the Loop iterations.

/* C Program to Count Total Number of Words in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100];
  	int i, totalwords;
  	totalwords = 1;
  	i = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	 	   	
  	while(str[i] != '\0')
	{
		if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
		{
			totalwords++;	
		} 
		i++;
	}	
	printf("\n The Total Number of Words in this String %s  = %d", str, totalwords);
	
  	return 0;
}
Please Enter any String :  Tutorial Gateway

 The Total Number of Words in this String Tutorial Gateway  = 2

Program to Count Total Number of Words in a String Example 3

This total words in a string program is the same as the first example, but this time we used the Functions concept to separate the logic.

/* C Program to Count Total Number of Words in a String */
 
#include <stdio.h>
#include <string.h>
 
int Count_TotalWords(char *str);
 
int main()
{
  	char str[100];
  	int totalwords;
  	totalwords = 1;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	 	   	
  	totalwords = Count_TotalWords(str);
  
	printf("\n The Total Number of Words in this String %s  = %d", str, totalwords);
	
  	return 0;
}

int Count_TotalWords(char *str)
{
	int i, totalwords;
  	totalwords = 1;
  	
	for(i = 0; str[i] != '\0'; i++)
	{
		if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
		{
			totalwords++;	
		} 
	}
	return totalwords;
}
Please Enter any String :  Learn c programming with examples

 The Total Number of Words in this String Learn c programming with examples  = 5

Comments are closed.