String

C++ 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

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

Count words of the string

C++ program to count the total number of words

Code to count the total number of words using for loop

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

  1. Declare and a character array as char str[100];
  2. Declare and initialize an integer variable as int i ,count=1;
  3. the user asked to enter a string to count words
  4. A for-loop is used to count total words of the given string using the count variable
  5. It is initialized as i=0, and checks condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
  6. Use an if condition to test if(str.charAt(i)!=’ ‘), if it is true,  count becomes count + 1(count=count+1)
  7. Finally, the program displays the total number of the words using the count variable

Code to count the total number of words using while  loop

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

  1. Declare a character array as char str[100];
  2. Declare and initialize an integer variable as int i ,count=1;
  3. The user asked to enter a string to count words
  4. A while-loop is used to count total words of the given string using the count variable
  5. It is initialized as i=0, and checks condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
  6. Use an if condition to test if(str.charAt(i)!=’ ‘), if it is true,  count becomes count + 1(count=count+1)
  7. i is incremented by 1
  8. Finally, the program displays the total number of the words using the count variable

 

Code to count the total number of words using do-while  loop

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

  1. Declare a character array as char str[100];
  2. Declare and initialize an integer variable as int i ,count=1;
  3. the user asked to enter a string to count words
  4. A do-while loop is used to count total words of the given string using the count variable
  5. It is initialized as i=0, and checks condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
  6. Use an if condition to test if(str.charAt(i)!=’ ‘), if it is true,  count becomes count + 1(count=count+1)
  7. i is incremented by 1
  8. Finally, the program displays the total number of the words using the count variable

 

Suggested for you

For loop in C++ language

while loop in C++ language

do-while loop in C++ language

 

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

 

 

Java program to count the total words in the given string
C program to count the total number of words in the given string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago