Home » Program to Count the Total Number of Characters in a String

Program to Count the Total Number of Characters in a String

by Online Tutorials Library

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

  1. Define a string.
  2. Define and initialize a variable count to 0.
  3. Iterate through the string till the end and for each character except spaces, increment the count by 1.
  4. 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#

You may also like