All occurences of character present in Given String


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 HashMap map=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()); } } }
Share:

No comments:

Post a Comment

Quote of the day

Popular Posts

Featured Post

Collection Framework Overview

Collection:- A collection (sometimes called a container) is simply an object that groups multiple elements into single unit. Collect...

Youtube Page

Facebook Page

Recent Posts

About

I am Shivaji Chandra and I'm a Computer Science Engineer. I love to write poems, jokes and quotes. click here →