Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Trees

Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

8,764 questions

12,137 answers

573 users

How to count characters occurrences in a string with Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        
        String str = "java apache software";
        
        int[] count = new int[255];
 		int length = str.length();
 
		for (int i = 0; i < length; i++) {
			count[str.charAt(i)]++;
		}
 
		for (int i = 0; i < 255; i++) {
		    if (count[i] != 0) {
		        System.out.format("%c : %d\n", (char)i, count[i]);
			}
		}
    }
}

/*
run:

  : 2
a : 5
c : 1
e : 2
f : 1
h : 1
j : 1
o : 1
p : 1
r : 1
s : 1
t : 1
v : 1
w : 1

*/

 




answered Nov 5 by avibootz
...