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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…