Write a Java Program to Count frequency or occurrence of all characters in string

By | 16.03.2017

Java Program to Count occurrence or frequency of all characters in string


Write a Java Program to Count frequency or occurrence of all the characters present in the string.Finding characters frequency of a String means calculating the occurrence of  each alphabet present in the string.

Since there’s no pre-build function present in Java for calculating the occurrence of the characters or alphabets so, we have to write a program which will give the frequency of characters present in the string.


Let’s get started : 

  • Firstly, the program will ask for an input string from the user.
  • Then start searching for the occurrence of all the characters present inside the string to find the frequency of all the characters in the string.
  • Then, Display the frequency of all the characters on the output screen as shown in the following program.

For example :

  • If the user has entered – “Elephant” .
  • Then the program will give –

e=2

l=1

p=1

h=1

a=1

n=1

t=1


ALGORITHM :


1. Start

2. Accept a sentence from the user.

3. Extract a character.

4. Count the no. of times it occurs in the sentence.

5. Print its frequency.

6. Repeat Steps 3 to 6 till all the frequencies are printed.

7. End


Following Java Program ask to the user to enter a string 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.

Below is the source code for Java Program to Count frequency or occurrence of all characters in string which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


import java.util.Scanner;

public class Freency {

    public static void main(String[] args) {
        
        int i,len,j;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter any string : ");
        String str = sc.nextLine();
        StringBuffer sb = new StringBuffer(str);
        len = sb.length();
        
        for(i=0;i<len;i++)
        {
            int c=1;
            for(j=0;j<len;j++)
            {
                if(sb.charAt(i)==sb.charAt(j) && (i!=j))
                {
                    c++;
                    sb.setCharAt(j, ' ');
                }
                
            }
            if(c>0 && sb.charAt(i)!=' ')
            {
                System.out.println("The "+sb.charAt(i)+" occurs " + c +" times.");
            }
        }
        
        
        
    }
    
}

OUTPUT : :


Enter any string : codezclub

The c occurs 2 times.
The o occurs 1 times.
The d occurs 1 times.
The e occurs 1 times.
The z occurs 1 times.
The l occurs 1 times.
The u occurs 1 times.
The b occurs 1 times.
3 2 votes
Article Rating
Category: Java Programming String Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Java programs

Using a for loop, traverse input string and increment the count of every character of input string. Finally, traverse the frequency array and print the frequency of every character. Check full java program to count characters of a string