codescracker


java

Java Program to Find Frequency of Characters



« Previous Program Next Program »


Find Frequency of Character

To find the frequency or to count the occurrence of all the characters present in the string/sentence, you have to ask to the user to enter the string, now start searching for the occurrence of all the characters present inside the string to find the frequency of all the characters in the string/sentence and display the frequency of all the characters on the output screen as shown in the following program.

Java Programming Code to Find Frequency of Characters in String

Following Java Program ask to the user to enter a string/sentence to find the frequency of all the characters present in the string and display the frequency of all the characters one by one on the screen:

/* Java Program Example - Find Frequency of Character */
		
import java.util.Scanner;

public class JavaProgram
{
   public static void main(String args[])
   {
        int ci, i, j, k, l=0;
        String str, str1;
        char c, ch;
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter a String : ");
        str=scan.nextLine();
        
        i=str.length();
        for(c='A'; c<='z'; c++)
        {
            k=0;
            for(j=0; j<i; j++)
            {
                ch = str.charAt(j);
                if(ch == c)
                {
                    k++;
                }
            }
            if(k>0)
            {
                System.out.println("The character " + c + " has occurred for " + k + " times");
            }
        }
   }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program find frequency of character

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test