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.
C++ program to count the total number of characters using for loop
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#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
1 2 3 4 5 |
Please enter the string for count characters Cpp language The total characters of the given string= 11 |
Approach
- Declare a character array as char str[100];
- Declare and initialize an integer variable as int i ,totChar=0;
- the user asked to enter a string to count character
- A for-loop is used to count total characters of the given string using the totChar variable
- It is initialized as i=0, and checks condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
- Use an if condition to test if(str.charAt(i)!=’ ‘), if it is true, totChar becomes totChar + 1(totChar=totChar+1)
- Finally, the program displays the total number of the character using the totChar variable
C++ program to count the total number of characters using while loop
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#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
1 2 3 4 5 |
Please enter the string for count characters C++ programming The total characters of the given string= 14 |
Approach
- Declare a character array as char str[100];
- Declare and initialize an integer variable as int i ,totChar=0;
- The user asked to enter a string to count character
- It is initialized an integer variable as i=0;
- A while-loop is used to count total characters of the given string using the totChar variable
- The while-loop checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
- Use an if condition to test if(str.charAt(i)!=’ ‘), if it is true, totChar becomes totChar + 1(totChar=totChar+1)
- Finally, the program displays the total number of the character using the totChar variable
C++ program to count the total number of characters using do-while loop
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#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
1 2 3 4 5 |
Please enter the string for count characters Code4example.com The total characters of the given string= 16 |
Approach
- Declare a character array as char str[100];
- Declare and initialize an integer variable as int i ,totChar=0;
- The user asked to enter a string to count character
- Initialized an integer variable as i=0;
- A do-while-loop is used to count total characters of the given string using the totChar variable
- The do-while-loop checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
- Use an if condition to test if(str.charAt(i)!=’ ‘), if it is true, totChar becomes totChar + 1(totChar=totChar+1)
- Finally, the program displays the total number of the character using the totChar variable