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

Thanks write a program that counts the frequency with which letters appear in a

ID: 3554214 • Letter: T

Question


Thanks

write a program that counts the frequency with which letters appear in a string of text. Character frequency is useful in deciphering simple encryption schemes (for example, as in The Gold Bug by Edgar Allen Poe). Prompt the user for a n input string, then count the number of times each letter appears in the string, treating upper and lowercase letters the same. No-letters are ignored. It may be useful to know that if myChar is a variable containing a uppercase letter, mychar A will eld the position of that letter thin the alphabet. For example, "C hile TZ A' is 25. Of course, 'A s 0 An efficient solution to this problem will need to use an array to hold the frequency count for each letter Sample run: Please enter a string: Hello world! Happy day! A 2 B 0 C 0 D 1 E 1 F 0 G 0 H 2 I 0 J 0 K 0 L 3 M 0 N 0 O 2 P 2 R 1 S 0 T 0 U 0 V 0 W 1 X 0 Y 2 Z 0

Explanation / Answer

Solution acc to the question:


public class NewClass1 {
    public static void main(String args[]){
        int[] count_arr = new int[26];     //this array will store count for each character with its index denotingnth character ie 0 for A, 1 fr b , 2 for C etc
        for(int i=0;i<26;i++)
            count_arr[i]=0;             //intitially setting count to 0 for each character
            System.out.println("Please enter the string : ");
            Scanner scanner = new Scanner(System.in);   //taking in the input string
            String s1 = scanner.nextLine();
            String s=s1.toUpperCase();
             for(int i = 0; i < s.length(); i++){
                char c = s.charAt(i);
                Integer val = (int)(c-'A');          //computig the array index for the character
                if(val >= 0 && val <= 26 )          //to ignore other ascii characters like !,@,$,%
                    count_arr[val]++;
            }
             for(int i=0;i<26;i++){
                 System.out.println((char)((int)'A'+i)+" "+count_arr[i]);
             }
    }
}


A Better solution:



public class NewClass {
    public static void main(String args[]){
        // Here in Map key is the character , and value is the no of times it has occured
        TreeMap<Character,Integer> map = new TreeMap<Character,Integer>(); //tree map is used because it'll be sorted acc t thekey thus would be easier in printing as A,B C,D in order
        for(int i=0;i<26;i++)
            map.put((char)((int)'A'+i),0);
        System.out.println("Please enter the string : ");
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.nextLine();
        String s=s1.toUpperCase();
        for(int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            Integer val = map.get(new Character(c));
            if(val != null){
            map.put(c, new Integer(val + 1));
            }
            else{
                map.put(c,1);
            }
        }
        for(char k : map.keySet())
            System.out.println(k+" "+map.get(k));
    }
   
}



Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote