Java Program to count the total number of characters in a string

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 numbers of characters present in the string are 19.

For programming, follow the algorithm given below:

Algorithm

  • STEP 1: START
  • STEP 2: DEFINE String string = “The best of both worlds”.
  • STEP 3: SET count =0.
  • STEP 4: SET i=0. REPEAT STEP 5 to STEP 6 UNTIL i<string.length
  • STEP 5: IF (string.charAt(i)!= ‘ ‘) then count =count +1.
  • STEP 6: i=i+1
  • STEP 7: PRINT count.
  • STEP 8: END

Program:

  1. public class CountCharacter    
  2. {    
  3.     public static void main(String[] args) {    
  4.         String string = “The best of both worlds”;    
  5.         int count = 0;    
  6.             
  7.         //Counts each character except space    
  8.         for(int i = 0; i < string.length(); i++) {    
  9.             if(string.charAt(i) != ‘ ‘)    
  10.                 count++;    
  11.         }    
  12.             
  13.         //Displays the total number of characters present in the given string    
  14.         System.out.println(“Total number of characters in a string: ” + count);    
  15.     }    
  16. }     

Output:

Total number of characters in a string: 19

Leave a Comment