In this article, we will discuss the concept of C++ program to count the total 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 given string using for loop in C++ language
Program 1
#include <iostream> #include <stdio.h> #include <conio.h> using namespace std; int main() { char str[100]; int i,count=1; cout << "Enter a String:" << endl; gets(str); for(i=0; str[i]!='\0'; ++i){ if(str[i]==' ') count++; } cout << "The total number of words in the given String: "<<count; getch(); return 0; }
When the above code is executed, it produces the following result
Enter a String: C++ is an OOP language The total number of words in the given String: 5
Approach
The program allows the user to enter a string thereafter It counts the total words of the given string using while loop in C++ language
Program 2
#include <iostream> #include <stdio.h> #include <conio.h> using namespace std; int main() { char str[100]; int i,count=1; cout << "Enter a String:" << endl; gets(str); i=0; while(str[i]!='\0'){ if(str[i]==' ') count++; ++i; } cout << "The total number of words in the given String: "<<count; getch(); return 0; }
When the above code is executed, it produces the following result
Enter a String: C++ language supports to the procedural way The total number of words in the given String: 5
Approach
The program allows the user to enter a string thereafter It counts the total words of the given string using do-while loop in C++ language
Program 3
#include <iostream> #include <stdio.h> #include <conio.h> using namespace std; int main() { char str[100]; int i,count=1; cout << "Enter a String:" << endl; gets(str); i=0; do{ if(str[i]==' ') count++; ++i; }while(str[i]!='\0'); cout << "The total number of words in the given String: "<<count; getch(); return 0; }
When the above code is executed, it produces the following result
Enter a String: C++ is a general-purpose programming language The total number of words in 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
10 simple ways to add two numbers in Java In this article, we will discuss…
Write a Python program to find the first n prime numbers In this article we…
Python: Calculate Average of odd and even in a list using loops In this post,…
Python: Average of Odd & Even Numbers from User Input In this post, we will…
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…