Hello Folks,
As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will learn to develop a java program to find out occurrence of individual characters in a given string.
Logic:
- To get input from user, we will use Scanner class.
- We will use Map interface concept here which deals in key-value pair.
- We will convert given string in to a char array.
- We will iterate char array index by index and will add in to Map if char does not exist already. If it exists we will increase value by one.
Java Program:-
package MakeSeleniumEasy; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Set; public class CountOfCharsInString { /** * @param inputString * @return Map* This method will save all char in a Map. */ public Map getCountOfChar(String inputString) { Map charDictionary = new HashMap (); // converting given string in to lower case String inoutCharInLowerCase=inputString.toLowerCase(); // Converting string in to char array char[] chrsInString = inoutCharInLowerCase.toCharArray(); // iterating char array /* * Map stores as key and value pairs where key should be unique. * We will consider character as a key. If key does not exists, we will add * in to key with value 1. If key exists, we will just increment the value by one. */ for(Character ch:chrsInString){ if(charDictionary.containsKey(ch)){ charDictionary.put(ch, charDictionary.get(ch)+1); } else { charDictionary.put(ch, 1); } } return charDictionary; } /** * @param charCountMap * This method will print all characters with number of occurrence in a string. * If char is white space, it will be shown as "White space". */ public void printCharWithCount(Map charCountMap) { Set keys = charCountMap.keySet(); for(Character ch:keys) { if(ch==' ') { System.out.println("White space" +" = "+charCountMap.get(ch)); } else { System.out.println(ch +" = "+charCountMap.get(ch)); } } } /** * @param charCountMap * This method can be used to print duplicate characters in a given string. */ public void printDuplicateCharsWithOccurance(Map charCountMap) { System.out.println("Duplicate characters are:"); Set keys = charCountMap.keySet(); for(Character ch:keys) { if(!(ch==' ')) { if(charCountMap.get(ch) > 1) { System.out.println(ch); } } } } public static void main(String a[]) { Scanner sc= new Scanner(System.in); System.out.println("Please provide a string to get count of characters: "); String inputString= sc.nextLine(); CountOfCharsInString object = new CountOfCharsInString(); Map map= object.getCountOfChar(inputString); object.printCharWithCount(map); object.printDuplicateCharsWithOccurance(map); } }
Output:
Please provide a string to get count of characters:
Amod Mahajan
White space = 1
a = 4
d = 1
h = 1
j = 1
m = 2
n = 1
o = 1
Duplicate characters are:
a
m
======================================================================
Please provide a string to get count of characters:
Make Selenium Easy
White space = 2
a = 2
s = 2
e = 4
u = 1
i = 1
y = 1
k = 1
l = 1
m = 2
n = 1
Duplicate characters are:
a
s
e
m
If you have nay doubt or suggestions feel free to comment.
If you like my posts, please like, comment, share and subscribe.
#HappyCoding
Any other method for this program without Map. Say only using CharAt().
Nice…)))
Thanks Ranjan.
Longest pallindromic Substring ? Program can you pls share bro ?
Ex : aabbracecardadmom
in the above ex longest pallindromic substring is racecar.
Can you pls share the code for that bro ? It will be helpful thanks.
Will do.