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 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

Java program: Find the frequency of each character in the string
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<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

Java program: Find the frequency of each character in the string
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<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

Java program: Find the frequency of each character in the string
Find the frequency of each character using while

 

find the frequency of  the each character in the string using do-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 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

Java program: Find the frequency of each character in the string
Find the frequency of each character using do while

 

 

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

 

 

By Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Leave a comment

Your email address will not be published. Required fields are marked *

10Shares