In this article, we will discuss the concept of C program to count the total number of words in the given string
In this post, we are going to learn how to count the total number of words in the given string in C programming language
The program allows the user to enter a string thereafter It counts the total words of the string using for loop in C language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { char str[100]; int count,i,words=1; printf("Enter a string for count words\n"); gets(str);//store the string vlue gets from user count=strlen(str); for(i=0; i<count; i++){ if(str[i]!=' ' && str[i+1]==' ') words=words+1; } printf("Total words of the given string: %d ",words); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string for count words C is a procedural language Total words of the given string: 5
Approach
The program allows the user to enter a string thereafter It counts the words of the string using while loop in C language
Program 2
#include <stdio.h> #include <stdlib.h> int main() { char str[100]; int count,i,words=1; printf("Enter a string for count words\n"); gets(str); count=strlen(str); i=0; while(i<count){ if(str[i]!=' ' && str[i+1]==' ') words=words+1; i++; } printf("Total words of the given string: %d ",words); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string for count words C is supporting structured programming Total words of the given string: 5
Approach
The program allows the user to enter a string thereafter It counts the words of the string using do-while loop in C language
Program 3
#include <stdio.h> #include <stdlib.h> int main() { char str[100]; int count,i,words=1; printf("Enter a string for count words\n"); gets(str); count=strlen(str); i=0; do{ if(str[i]!=' ' && str[i+1]==' ') words=words+1; i++; }while(i<=count); printf("Total words of the given string: %d ",words); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string for count words C language is not supported OOP concept Total words of the given string: 7
Approach
Suggested for you
Similar post
Java program to count the total number of characters in the given string
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
Python program to count the total number of words in the given string
Java program to count the total number of words in the given string
C++ program to count the total number of words in the given string
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…