C Program to Count Number of Words in a String

C Program to Count Number of Words in a String

C program to count a number of words in a string; Through this tutorial, we will learn how to count number of words in a given string using for loop, while loop, and function in c program.

Programs to Count Number of Words in a String in C

Let’s use the following programs to count number of words in a given string using for loop, while loop, and function in c:

  • C Program to Count Number of Words in a String using for loop
  • C Program to Count Number of Words in a String using while loop
  • C Program to Count Number of Words in a String using function

C Program to Count Number of Words in a 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] != '
/* 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;
}
'; 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; }

The output of the above c program; as follows:

Please Enter any String :  hello developer
The Total Number of Words in this String hello developer  = 2

C Program to Count Number of Words in a String using while 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;
  	i = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	 	   	
  	while(str[i] != '
/* 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;
}
') { 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; }

The output of the above c program; as follows:

Please Enter any String :  hy dud
The Total Number of Words in this String hy dud  = 2

C Program to Count Number of Words in a String using function

/* 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] != '
/* 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;
}
'; i++) { if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t') { totalwords++; } } return totalwords; }

The output of the above c program; as follows:

Please Enter any String :  hello dear sir
The Total Number of Words in this String hello dear sir  = 3

Recommended C Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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