In this article, we will discuss the concept of the Java program: find the frequency of each character in the given string
In this post, we are going to learn how to find the frequency of the each character of the given string in Java programming language
The program allows the user to enter a String and then it finds the frequency of the each character in the given string using for loop in Java programing language
program 1
//program to count the frequency of each character of the given string import java.util.Scanner; public class Count_frequency{ public static void main(String args[]){ //variable declaration String str=" "; int count=0,length=0,num=0; //vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String "); str=scan.nextLine(); length=str.length(); for(int i=0; i<length; i++){ count=0; for(int j=0; j<length; j++){ if(str.charAt(j)==str.charAt(i)) count++; } for(int k=0; k<i; k++){ if(str.charAt(k)==str.charAt(i)) num=1; } if(num!=1) System.out.print(str.charAt(i)+" is occured: "+count+"times"+"\n"); num=0; } } }
When the above code is executed, it proces the following result
The program allows the user to enter a String and then it finds the frequency of the each character in the given string using while loop in Java programing language
program 2
//program to count the frequency of each character of the given string import java.util.Scanner; public class Count_frequency1{ public static void main(String args[]){ //variable declaration String str=" "; int count=0,length=0,num=0; //vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String "); str=scan.nextLine(); length=str.length(); int i=0; while(i<length){ count=0; int j=0; while(j<length){ if(str.charAt(j)==str.charAt(i)) count++; j++; } int k=0; while(k<i){ if(str.charAt(k)==str.charAt(i)) num=1; k++; } if(num!=1) System.out.print(str.charAt(i)+" is occured: "+count+"times"+"\n"); num=0; i++; } } }
When the above code is executed, it proces the following result
The program allows the user to enter a String and then it finds the frequency of the each character in the given string using do- while loop in Java programing language
program 3
//program to count the frequency of each character of the given string import java.util.Scanner; public class Count_frequency2{ public static void main(String args[]){ //variable declaration String str=" "; int count=0,length=0,num=0; //vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String "); str=scan.nextLine(); length=str.length(); int i=0; do{ count=0; int j=0; do{ if(str.charAt(j)==str.charAt(i)) count++; j++; }while(j<length); int k=0; do{ if(str.charAt(k)==str.charAt(i)) num=1; k++; }while(k<i); if(num!=1) System.out.print(str.charAt(i)+" is occured: "+count+"times"+"\n"); num=0; i++; }while(i<length); } }
When the above code is executed, it proces the following result
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
Java program to count the frequebcy of a character of a string
C program to count the frequebcy of a character of a string
Python program to count the frequebcy of a character of a string
C++ program to count the frequebcy of a character of a 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…