Count total number of times each alphabet appears in the string java program code with example

The common technical interview question in java is to count the occurrence of  a specific word in the string but what if interviewer ask to count the total number of times each alphabet appears in the string . That the question, whose logic you need to think at that point of time and sadly there is no  built in  java method to give you the direct answer by passing the input string .

Read Also :    Count number of  words in the String with Example

So in order to be prepared for such type of questions we have shared the program code below .

Input string  :       "Alive is awesome"

Output           :      A=1,l=1,i=2,v=1,e=3, =2,s=2,a=1,w=1,o=1,m=1

Note :  The result is case sensitive , so if the alphabet is in uppercase and lowercase are treated as different
             for example , Please see above  the alphabet which is texted in Red

Read Also :    Find first Non-Repeated Character in the String

    Pseudo code/Logic  for the Program

   *  We store the input string in an character array (below z) .(We want to create a character array z without                                                                                                any duplicate alphabet )
   *  We start traversing the string in array z[i].
           for i=0 to i<z.length

               Store the character at index i that is z[i] in temporary variable ch
                               for  z[i+1] to z.length  
                                           if  ch is equals to the any element of the rest of the array characters          
                                               then   move the  cursor  to next index and reduce the length of the string by 1
                                               set s[k]=s[k+1] // deleting the alphabet which is already in array z
                                               set j=i    

    *  Create a new array of integers to store the count of  total number of appearances of alphabets   in the           string     (below t)

    *  Compare every element of string s with   the every element of character array z
                      if both are equal then
                             increase count by 1
         Add the final count value to the array t (which is used to store the final count of  total number of                                                                                occurrences of the alphabet in the string )

    *  Print  the count of the alphabet                                  

Read Also :      Abstraction meaning explained in simple words

Demo :

count total number of times each alphabet appears in the string java program

Code :

public class  AlphabetFrequencyString {
    static int i,j,k,c=0,w;
    static char m;                      //we can only define static for variables and fns not for arrays
    public static void main(String[] args) {
        System.out.println("Input string is : ");
        System.out.println("Alive is awesome");
        System.out.println("Output :");
        frequencycount("Alive is awesome");
    static void frequencycount(String s)
        char[] z=new char[s.length()];
            char ch=z[i];
        int[] t=new int[w];
            t[i]=c ;

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry