Alphabet Occurance in a given String

Count Number of characters in a Given String
SOURCE : AlphabetOccurance.java


import java.util.Map;
import java.util.TreeMap;

public class AlphabetOccurance {

/**
* @param args
*/
public static void main(String[] args) {
String s = "iam in india";
String str = s.replaceAll(" ", "");

TreeMap tmap = new TreeMap<>();

for (Character c : str.toCharArray()) {

Integer freq = tmap.get(c);
if (freq == null) {

freq = 0;
}

tmap.put(c, freq + 1);

}

System.out.println(tmap);

for (Map.Entry m : tmap.entrySet()) {

if (m.getValue() > 1) {

System.out.println(m.getKey() + " " + m.getValue());
}

}
}

}




OUTPUT

{a=2, d=1, i=4, m=1, n=2}
a 2
i 4
n 2






Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment