In this article, we will discuss the concept of the Java program to count the total number of characters in the given string
In this post, we are going to learn how to count the total number of characters in the given string of Java programming language
The program allows the user to enter a string and then It counts the characters of the given string using for loop in Java language
Program 1
import java.util.Scanner; public class Count_WordOfString{ public static void main(String args[]){ String str; //variable declaration int count=0; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String for count characters "); str=scan.nextLine(); //count each character without space for(int i=0; i<str.length(); i++){ if(str.charAt(i)!=' '){// this condition is used to avoid counting space count++; } } //display the total number of characters in te given string System.out.print("The total number of character in a string :"+count); } }
When the above code is executed, it produces the following result
Case 1
Enter the String for count characters Java language The total number of character in a string:12
Case 2
Enter the String for count characters Java programming language The total number of character in a string:23
Approach
The program allows the user to enter a string and thereafter It counts the characters of the string using while loop in Java language
Program 2
import java.util.Scanner; public class Count_WordOfString1{ public static void main(String args[]){ String str; //variable declaration int count=0; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String for count characters "); str=scan.nextLine(); //count each character without space int i=0; while(i<str.length()){ if(str.charAt(i)!=' '){// this condition is used to avoid counting space count++; } i++; } //display the total number of characters in te given string System.out.print("Total number of character in a string:"+count); } }
When the above code is executed, it produces the following result
Enter the String for count characters Java is an OOP language The total number of character in a string:19
Approach
The program allows the user to enter a string and then It counts the characters of the string using do-while loop in Java language
Program 3
import java.util.Scanner; public class Count_WordOfString2{ public static void main(String args[]){ String str; //variable declaration int count=0; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String for count characters "); str=scan.nextLine(); //count each character without space int i=0; do{ if(str.charAt(i)!=' '){// this condition is used to avoid counting space count++; } i++; }while(i<str.length()); //display the total number of characters in te given string System.out.print("Total number of character in a string:"+count); } }
When the above code is executed, it produces the following result
Enter the String for count characters Java easy language for beginners The total number of character in a string:28
Approach
Suggested for you
for loop in Java language
while loop in Java language
do-while loop in Java language
Similar post
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…