In this article, we will discuss the concept of the C++ program to count the total number of characters in the given string including space
In this post, we are going to learn how to count the total number of character including space of the given String in C ++ programming language
The program allows the user to enter a String and then it counts and display the number of characters including space of the given string using for loop in C++ programing language
Program 1
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { char str[100]; int i,totChar; totChar=0; cout<<"Please enter the string for count characters\n"; gets(str); //count characters of a string wit out space for(i=0; str[i] != '\0'; i++){ if(str[i]!='\0') { totChar++; } } cout<<"The total characters of the given string= "<<totChar; getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string for count characters Cpp is a basic language The total characters of the given string=23
Approach
The program allows the user to enter a String and then it counts and display the total number of characters including space of the given string using while loop in C++ programing language
Program 2
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { char str[100]; int i,totChar; totChar=0; cout<<"Please enter the string for count characters\n"; gets(str); //count characters of a string wit out space i=0; while(str[i] != '\0'){ if(str[i]!='\0') { totChar++; } i++; } cout<<"The total characters of the given string= "<<totChar; getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string for count characters Cpp is the best language The total characters of the given string=24
Approach
The program allows the user to enter a String and then it counts and display the number of characters including space of the given string using do-while loop in C++ programing language
Program 3
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { char str[100]; int i,totChar; totChar=0; cout<<"Please enter the string for count characters\n"; gets(str); //count characters of a string wit out space i=0; do{ if(str[i]!='\0') { totChar++; } i++; }while(str[i] != '\0'); cout<<"The total characters of the given string= "<<totChar; getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string for count characters
Cpp is an OOP supported language
The total characters of the given string=32
Approach
Suggested for you
Similar post
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
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
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,…