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