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:
- Start
- Declare a string.
- Initialize the string.
- Declare a variable to count the total number of characters in the given string and initialize it to 0.
- Use a for loop to calculate the same.
- Use an if condition to avoid counting space.
- Increment the count each time a character encounters.
- Print the total number of characters in the given string.
- 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
- Start.
- Declare a variable to count the total number of characters in the given string and initialize it to 0.
- Declare a string and ask the user to initialize it.
- Use a while loop to calculate the total characters in the given string.
- Use an if condition to avoid counting spaces.
- Increment the count variable if a character encounters.
- Print the total number of characters in the given string.
- 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
- Start.
- Declare a string.
- Declare a variable to count the total number of characters in the string and initialize it to 0.
- Ask the user to initialize the string.
- Use a do-while loop to calculate the total number of characters in the string.
- The do-while loop checks the condition whether i<str.length(); and executes the loop until the given condition becomes true.
- Use an if condition to avoid counting space.
- Print the total number of characters in the given string.
- 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