Javatpoint Logo
Javatpoint Logo

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#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA