Home >>Java Programs >Java Program to count the total number of characters in a string
In this example, we will create a java program to count the number of characters present in the string.
To count the number of characters present in the given string, we will iterate through the string and count the characters.
public class Main
{
public static void main(String[] args)
{
String string = "Hello everyone... Welcome here";
int count = 0;
for(int i = 0; i < string.length(); i++)
{
if(string.charAt(i) != ' ')
count++;
}
System.out.println("Total number of characters in a string: " + count);
}
}