Monday , 27 February 2017
Home » ICSE Important Programs » Program to find frequency of every alphabet in a String

Program to find frequency of every alphabet in a String

Question:

Write a program to input a string (word). Convert it into lowercase letters. Count and print the frequency of each alphabet present in the string. The output should be given as:
Sample Input: Alphabets
Sample Output:
==========================
Alphabet             Frequency
==========================
a                              2
b                              1
e                              1
h                              1
l                               1
p                              1
s                              1
t                               1

Programming Code:

/**
* The class AlphabetFreq inputs a string and counts the frequency of each alphabet present in it
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class AlphabetFreq
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any string: ");
        String s = br.readLine();
 
        s=s.toLowerCase(); //converting the string into lowercase
        int l=s.length(); //finding the length of the string
 
        char ch;
        System.out.println("Output:");
        System.out.println("=========================="); //this is just for styling the look of the output
        System.out.println("Alphabet\tFrequency");
        System.out.println("==========================");
 
        /* Counting frequency of alphabets begins below */
        int count=0;
        for(char i='a'; i<='z'; i++)
            {
                count = 0;
                for(int j=0; j<l; j++)
                {
                    ch=s.charAt(j); //extracting characters of the string one by one
                    if(ch==i) //first checking the whole string for 'a', then 'b' and so on
                        count++; //increasing count of those aplhabets which are present in the string
                }
                if(count!=0)//printing only those alphabets whose count is not '0'
                {
                    System.out.println(i+"\t\t"+count);
                }
            }
    }
}

Output:

Enter any string: ilovejavaforschool

Output:
==========================
Alphabet             Frequency
==========================
a                               2
c                               1
e                              1
f                               1
h                              1
i                                1
j                                1
l                                2
o                               4
r                                1
s                               1
v                               2

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

11 comments

  1. how can we count words instead if charcters?

    • System.out.print("Enter any sentence : ");
      String s = br.readLine();
      s = s + " ";
      int l = s.length();
      int c = 0;
      for(int i=0;i<l;i++)
              {
                  if(s.charAt(i)==' ')
                  {
                      c++;
                  }
              }
      System.out.println("Number of words = "+c);
  2. Thank you …..very much very useful ……i like this website

  3. Thanku very much very useful

  4. What is \t?How is it used here?

  5. thanx awesome code

  6. THIS IS THE REAL TEACHER

  7. this really helped me alot thanks a ton!!!!

  8. awesome……..stuff…thanx really helped me

  9. awesome……..stuff…thanx really helped me..

Leave a Reply

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