Explanation
In this program, we need to count the number of characters present in the string.
The best of both worlds
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] != ' '.
Input:
string = "The best of both worlds"
Output:
Total number of characters in a string: 19
Python
Output:
C
Output:
JAVA
Output:
C#
Output:
PHP
Output: