Signup/Sign In

Java Program to Count the Total Number of Characters in a String

In this tutorial, we will learn how to calculate the total number of characters in a string. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.

Input: Enter the string: Hello World

Output: The total number of characters in the string is: 10

The above problem can be solved in three ways:

Approach 1: Using a for loop

Approach 2: Using a while loop

Approach 3: Using a do-while loop

Let us look at each of these approaches separately.

Program 1: To Calculate the Total number of Characters in a String

In this program, we will find the total number of characters in a string with pre-defined values. Here, we will use a for loop to calculate the total number of characters in the string.

Algorithm:

  1. Start
  2. Declare a string.
  3. Initialize the string.
  4. Declare a variable to count the total number of characters in the given string and initialize it to 0.
  5. Use a for loop to calculate the same.
  6. Use an if condition to avoid counting space.
  7. Increment the count each time a character encounters.
  8. Print the total number of characters in the given string.
  9. Stop.

Below is the code for the same.

/*Java Program to count the total number of characters in a string using a for loop*/
public class Main
{
  public static void main(String[] args) 
  {    
        String str = "Hello World";    
        int count = 0;    
        System.out.println("The entered string is: "+str);    
        //Count the characters in the string except space    
        for(int i = 0; i < str.length(); i++) 
        {    
            if(str.charAt(i) != ' ')    
                count++;    
        }                
        //Displays the total number of characters in the string    
        System.out.println("Total number of characters in the string: " + count);    
    }      
}


The entered string is: Hello World
Total number of characters in the string: 10

Program 2: Calculate the Total number of Characters in a String

In this program, we will find the total number of characters in a string with user-defined values. Here, we will ask the user to enter the values and then will use a while loop to calculate the total number of characters in the string.

Algorithm

  1. Start.
  2. Declare a variable to count the total number of characters in the given string and initialize it to 0.
  3. Declare a string and ask the user to initialize it.
  4. Use a while loop to calculate the total characters in the given string.
  5. Use an if condition to avoid counting spaces.
  6. Increment the count variable if a character encounters.
  7. Print the total number of characters in the given string.
  8. Stop.

Below is the code for the same in Java language.

/*Java Program to count the total number of characters in a string using a while loop*/
import java.util.*;
public class Main
{
  public static void main(String[] args) 
  {    
        //Take input from the user
        Scanner sc=new Scanner(System.in);    
        int count = 0,i=0;    
        System.out.print("Please Enter a String to Count Characters =  ");
		String str = sc.nextLine();
		//Use a while loop to calculate the total characters in the string
		while(i < str.length())
		{
			if(str.charAt(i) != ' ') 
			{
				count++;
			}
			i++;
		}		
		System.out.println("\nThe Total Number of Characters  =  " + count);        
    }      
}


Please Enter a String to Count Characters = Calculate the string length
The Total Number of Characters = 24

Program 3: Calculate the Total number of Characters in a String

In this program, we will find the total number of characters in a string with user-defined values. Here, we will ask the user to enter the values and then we will use a do-while loop to calculate the total number of characters in the string.

Algorithm

  1. Start.
  2. Declare a string.
  3. Declare a variable to count the total number of characters in the string and initialize it to 0.
  4. Ask the user to initialize the string.
  5. Use a do-while loop to calculate the total number of characters in the string.
  6. The do-while loop checks the condition whether i<str.length(); and executes the loop until the given condition becomes true.
  7. Use an if condition to avoid counting space.
  8. Print the total number of characters in the given string.
  9. Stop.

Below is the code for the same in Java language.

/*Java Program to count the total number of characters in a string using a do-while loop*/
import java.util.*;
public class Main
{
  public static void main(String[] args) 
  {    
        //Take input from the user
        Scanner sc=new Scanner(System.in);    
        int count = 0,i=0;    
        System.out.println("Please Enter a String to Count Characters =  ");
		String str = sc.nextLine();
		//Use a while loop to calculate the total characters
		do 
		{
           if(str.charAt(i)!=' ')
           {
              // this condition is used to avoid counting space
              count++;
           }
            i++;
        }while(i<str.length());
        //Print the total number of characters in the given string
        System.out.print("The total number of character in a string:"+count);
    }      
}


Please Enter a String to Count Characters = This is an example of a do-while loop
The total number of character in a string:30



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.