Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Frequently Asked Java Program 08: Program to find occurrence of individual characters in a given string.

Posted on 03/16/2025 By admin

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:

  1. To get input from user, we will use Scanner class.
  2. We will use Map interface concept here which deals in key-value pair.
  3. We will convert given string in to a char array.
  4. 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 MahajanWhite space = 1a = 4d = 1h = 1j = 1m = 2n = 1o = 1Duplicate characters are:a

m

Uncategorized

Post navigation

Previous Post: WebDriverManager – Get Rid Of Compatability Issues Between Browser And Drivers
Next Post: selenium testng tutorials

Related Posts

March 28, 2019 – Make Selenium Easy Uncategorized
TestNG Tutorials 48: How to Pass Parameters of Different Datatypes in TestNG | Make Selenium Easy Uncategorized
January 20, 2019 – Make Selenium Easy Uncategorized
What will happen if we pass NULL as argument in sendKeys() method Of Selenium WebDriver | Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
TestNG Tutorials 64: Dependency in TestNG – ignoreMissingDependencies – Another Way of Achieving Soft Dependencies Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark