String

C++ program: Count word, characters and space of a string

C++ program: Count word, characters and space of a string

In this article, we will discuss the concept of the C++ program: Count word, characters and space of a string

In this post, we are going to learn how to count the total number of words, character and Space of the given string in C++ programming language

Count words, characters and space

C++ program: Count word, characters and space of a string

Count words, character and Space using for loop

The program allows the user to enter a String and then it counts and display the total number of words, character and Space of the given string using for loop in C programing language

Program 1

// C++ program: Count word, characters and space of a string
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
    char str[100];//declare and initialize char array
    int i;
    int words=1,characters=0,space=0;
    cout<<"Please enter the string \n";
    gets(str);

    for(i=0; str[i] != '\0'; i++){
            if(str[i]!=' '){//check characters
                characters++;
            }
             else if(str[i]==' ' || str[i] != '\n' || str[i] != '\t'){ //check words
                words++;
            }
    }
cout<<"\nTotal words: "<<words;   //display total numbers of words
cout<<"\nTotal characters: "<<characters; //display total numbers of characters
cout<<"\nSpace: "<<(words-1);  ////display total numbers of space
getch();
    return 0;
}

When the above code is executed, it produces the following result

Please enter the string
Cpp language tutorials code for coding

Total words: 6
Total characters: 33
Space: 5

 

Approach

  1. Declare a Character array as char str[100];
  2. Declare and initialize integer variables as int words=1,space=0,caracters=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A for loop is used to count total characters of the given string.
  6. It is initialized as i=0, checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str[i]!=’ ‘), if it is true,  The characters becomes characters + 1( characters = characters +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The words becomes words+1(words=words+1);
  9. Finally, the program displays the total number of the words, characters and Space of the given string

 

 

Count word, characters and space using while loop

The program allows the user to enter a String and then it counts and display the total number of words, character and Space of the given string using while loop in C programing language

Program 2

#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
    char str[100];
    int i;
    int words=1,characters=0,space=0;
    cout<<"Please enter the string \n";
    gets(str);

    i=0;
    while(str[i] != '\0'){
            if(str[i]!=' '){ // check characters
                characters++;
            }
             else if(str[i]==' ' || str[i] != '\n' || str[i] != '\t'){ //check words
                words++;
            }
             i++;
    }
cout<<"\nTotal words: "<<words;//display total number of words
cout<<"\nTotal characters: "<<characters; //display total number of characters
cout<<"\nSpace: "<<(words-1);//display total number of spaces
getch();
    return 0;
}

When the above code is executed, it produces the following result

Please enter the string
Learn basic Cpp tutorials

Total words: 4
Total characters: 22
Space: 3


Approach

  1. Declare a Character array as char str[100];
  2. Declare and initialize integer variables as words=1,space=0,caracters=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A while loop is used to count total characters of the given string.
  6. It is initialized as i=0, checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str[i]!=’ ‘), if it is true,  The characters becomes characters + 1( characters = characters +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The words becomes words+1(words=words+1);
  9. Finally, the program displays the total number of the words, characters and Space of the given string

 

 

Count word, characters and space using do-while loop

The program allows the user to enter a String and then it counts and display the total number of words, character and Space of the given string using do-while loop in C programing language

Program 3

#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
    char str[100];
    int i;
    int words=1,characters=0,space=0;
    cout<<"Please enter the string \n";
    gets(str); //count characters of a string wit out space

    i=0;
    do{
            if(str[i]!=' '){ // check characetrs
                characters++;
            }
             else if(str[i]==' ' || str[i] != '\n' || str[i] != '\t'){ // check words
                words++;
            }
             i++;
    }while(str[i] != '\0');
cout<<"\nTotal words: "<<words; //display total number of words
cout<<"\nTotal characters: "<<characters; //display total number of characters
cout<<"\nSpace: "<<(words-1); //display total number of spaces
getch();
    return 0;
}

When the above code is executed, it produces the following result

Please enter the string
C++ is an OOP language

Total words: 5
Total characters: 18
Space: 4

 

Approach

  1. Declare a Character array as char str[100];
  2. Declare and initialize integer variables as words=1,space=0,caracters=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A do-while loop is used to count total characters of the given string.
  6. It is initialized as i=0, checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str[i]!=’ ‘), if it is true,  The characters becomes characters + 1( characters = characters +1);
  8. When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The words becomes words+1(words=words+1);
  9. Finally, the program displays the total number of the words, characters and Space of the given string

 

 

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  upper case lower case numeric and special character

C program to count the total number of  upper case lower case numeric and special character

Python program to count the total number of  upper case lower case numeric and special character

C++ program to count the total number of  upper case lower case numeric and special character

 

Java program to count the total number of characters in the given string including space

C program to count the total number of characters in the given string including space

Python program to count the total number of characters in the given string including space

 

 

Count words,characters and space in Java language
C program: Count word,characters and space of a 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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

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