85
Program to count the total number of characters in a string
Explanation
In this program, we need to count the number of characters present in the string.
To count the number of characters present in the string, we will iterate through the string and count the characters. In above example, total number of characters present in the string are 19.
Algorithm
- Define a string.
- Define and initialize a variable count to 0.
- Iterate through the string till the end and for each character except spaces, increment the count by 1.
- To avoid counting the spaces check the condition i.e. string[i] != ‘ ‘.
Solution
Python
Output:
Total number of characters in a string: 19
C
Output:
Total number of characters in a string: 19
JAVA
Output:
Total number of characters in a string: 19
C#
Output:
Total number of characters in a string: 19
PHP
Output:
Total number of characters in a string: 19
Next Topic#