In this article, we will discuss the concept of C++ program to count the total number of characters in the given string
In this post, we are going to learn how to count the total number of characters in the given string in C++ programming language
The program allows the user to enter a string and thereafter It counts the total characters of the given string without space using for loop in C++ 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);//gets and store string from useer //count characters of a string wit out space for(i=0; str[i] != '\0'; i++){ if(str[i]!=' ')// this condition is used to avoid counting space { 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 language The total characters of the given string= 11
Approach
The program allows the user to enter a string and thereafter It counts the characters of the string without space using while loop in C++ 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]!=' ')// this condition is used to avoid counting space { 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 C++ programming The total characters of the given string= 14
Approach
The program allows the user to enter a string and thereafter It counts the characters of the string without space using do-while loop in C++ 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]!=' ')// this condition is used to avoid counting space { 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 Code4coding.com The total characters of the given string= 15
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
Python program to count the total number of characters in the given string
How to find reverse number using method In this article, we will discuss the concept…
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…