import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.concurrent.SynchronousQueue; public class StringOccurence { public static void main(String[] args) { String str; Scanner sc; System.out.print("Enter the string :"); sc=new Scanner(System.in); str=sc.next(); //Method Call printOccurence(str); } //method for printing occurences private static void printOccurence(String str) { //creating one map HashMapmap=new HashMap (); char[] charArr=str.replaceAll("\\s","").toLowerCase().toCharArray(); //logic to detect and store all occurences for(char c:charArr) { if(map.containsKey(c)) { map.put(c,map.get(c)+1); } else { map.put(c,1); } } //logics to print all the occurences for(Map.Entry entry:map.entrySet()) { System.out.print("Key :"+entry.getKey()+" "+"Value :"+entry.getValue()); } } }
Home »
» All occurences of character present in Given String
No comments:
Post a Comment