Java program: Find the frequency of each character in the string
Java program: Find the frequency of each character in the string
In this article, we will discuss the concept of theJava 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
Find the frequency of each character in the string
Code to find the frequency of the each character in the given string
find the frequency of the each character in the string using for loop
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
When the above code is executed, it proces the following result
Find the frequency of each character using for
find the frequency of the each character in the string using while loop
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
When the above code is executed, it proces the following result
Find the frequency of each character using while
find the frequency of the each character in the string using do-while loop
//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
When the above code is executed, it proces the following result
Find the frequency of each character using do while