Frequently Asked Java Program 27: Java Program to Find Occurrence of Any Char in a Given String Without Iterating
Problem Statement:
Find occurrence of given char (Ignore case) in given string without iterating through string.
Input – Make Selenium Easy
Char – ‘M’
Output- Occurrence of M is 2.
Problem Solution:
We need to solve this problem without iterating through string in search of character. Its simple.
Logic step by step:
- FInd the initial length of given string.
- Replace desired char by empty character.
- Find the length of string after replacement.
- FInd the difference between initial and current length.
- The difference is occurrence of asked char in string.
Java Programs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package StringPrograms; import java.util.Scanner; public class CountOfCharInGivenString { public static int countOfChar(String inputString, char charToFind) { //Converting string into same case inputString = inputString.toUpperCase(); // Converting char to upper case String charString= Character.toString(charToFind).toUpperCase(); // Find length of actual string int lengthBeforeReplacingSpace = inputString.length(); // Replace char to be searched for by empty character inputString = inputString.replace(charString, ""); //FInd the new length after replacement int lengthAfterReplacingSpace = inputString.length(); // FInd the difference int countOfSpace = lengthBeforeReplacingSpace- lengthAfterReplacingSpace; // return the difference which is actually occurrence of char return countOfSpace; } public static void main(String[] args) { // User input for the string to know length Scanner sc = new Scanner(System.in); System.out.println("Please enter the string :"); String userInput = sc.nextLine(); System.out.println("Please enter the char to find occurrence in : "+userInput); char charToFind = sc.nextLine().charAt(0); sc.close(); System.out.println("You entered: " + userInput); System.out.println("Char to be found: " + charToFind); System.out.println("Count of "+charToFind+" is :" + countOfChar(userInput,charToFind)); } } |
Output:
Please enter the string : Make Selenium Easy Please enter the char to find occurrence in : Make Selenium Easy m You entered: Make Selenium Easy Char to be found: m Count of m is :2 ================================================================= Please enter the string : Autoation Tester Please enter the char to find occurrence in : Autoation Tester T You entered: Autoation Tester Char to be found: T Count of T is :4 =================================================================
#HappyCoding
Author: Amod Mahajan
My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning
We can also use HashMap to get the occurrences of a given char
There is a another easy way for the same question:
Please give your suggesion on this:
package basicJava;
import java.util.Scanner;
public class OccuOfCharInString {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(“Enter the String :”);
Scanner scan = new Scanner(System.in);
String str=scan.nextLine();
System.out.println(“Enter the chat need to find the occurance:”);
char ch=scan.nextLine().charAt(0);
int count=0;
for(int i=0;i<str.length();i++) {
if(ch==str.charAt(i)) {
count++;
}
}
System.out.println("The total number of occurance of given char is "+count);
}
}
Very easy and straight forward. As we say and know there are always multiple answers to a question.