Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to create an ASCII frequency table from a string in Java

1 Answer

0 votes
import java.util.HashMap;
import java.util.Map;

public class ASCIIFrequency {
    public static Map<Character, Integer> getASCIIFrequency(String str) {
        Map<Character, Integer> frequencyTable = new HashMap<>();
        
        for (char c : str.toCharArray()) {
            frequencyTable.put(c, frequencyTable.getOrDefault(c, 0) + 1);
        }
        
        return frequencyTable;
    }

    public static void main(String[] args) {
        String input = "java c c++ c# rust python php";
        
        Map<Character, Integer> frequencyTable = getASCIIFrequency(input);
        
        System.out.println(frequencyTable);
    }
}



/*
run:

{ =6, a=2, c=3, #=1, h=2, j=1, +=2, n=1, o=1, p=3, r=1, s=1, t=2, u=1, v=1, y=1}

*/

 



answered Oct 16, 2024 by avibootz
edited Oct 16, 2024 by avibootz

Related questions

1 answer 616 views
1 answer 208 views
1 answer 97 views
1 answer 99 views
1 answer 89 views
1 answer 102 views
1 answer 116 views
...