Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that reads a string from the user, and then prints out the

ID: 3757876 • Letter: W

Question

Write a Java program that reads a string from the user, and then prints out the count of each letter (regardless of case) as shown below. Note that you need to use separate counter for each letter, that is 26 counters, named as aCount, bCount, cCount, dCount, …. Print out only the counters that count the letters used in the string (that is, counters with values > 0 only). Do not printer counters with 0 values. Use proper labels for the outputs as shown in the examples below.

Entered String: This is a test input.

Letter A: 1

Letter E: 1

Letter H: 1

Letter I: 3

Letter N: 1

Letter P: 1

Letter S: 3

Letter T: 4

Letter U: 1

Explanation / Answer

public class StringAlp {  

    static int i,j,k,c=0,w;  
    static char m;                  
  
    public static void main(String[] args) {
        System.out.println("Input string is : ");
        System.out.println("Helloworld"); // Print String name here
        System.out.println("");
        System.out.println("");
        System.out.println("Output :");
        frequencycount("Helloworld"); // Enter String name here
    }
  
  
    static void frequencycount(String s)  
    {      
        char[] z=new char[s.length()];
        for(w=0;w<s.length();w++)
        z[w]=s.charAt(w);
        for(i=0;i<w;i++)
        {
            char ch=z[i];
            for(j=i+1;j<w;j++)
            {
                if(z[j]==ch)
                {
                    for(k=j;k<(w-1);k++)
                    z[k]=z[k+1];
                    w--;
                    j=i;
                }
            }
        }
      
        int[] t=new int[w];
        for(i=0;i<w;i++)
        {
            for(j=0,c=0;j<s.length();j++)
            {
                if(z[i]==s.charAt(j))
                c++;
            }
            t[i]=c ;
            System.out.println("Letter "+z[i]+":"+c);
        }
    }
  
}

Output is

Letter H: 1
Letter e: 1
Letter l: 3
Letter o: 2
Letter w: 1
Letter r: 1
Letter d: 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote