Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Frequently Asked Java Program 27: Java Program to Find Occurrence of Any Char in a Given String Without Iterating

Posted on 02/19/2025 By admin

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:

  1. FInd the initial length of given string.
  2. Replace desired char by empty character.
  3. Find the length of string after replacement.
  4. FInd the difference between initial and current length.
  5. The difference is occurrence of asked char in string.

Java Programs:

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:

[java] 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 =================================================================

[/java]

Uncategorized

Post navigation

Previous Post: Postman Tutorial Part 41 – Dynamic Variables in Postman
Next Post: Selenium Interview Questions: Why Do Not We Switch Back To Driver After Handling An Alert

Related Posts

Postman Tutorial Part 11 – Sending DELETE Request in Postman Uncategorized
amod mahajan make selenium easy Uncategorized
getOrDefault in java Uncategorized
How to load a URL in browser without using get() or navigate() method in Selenium Uncategorized
TestNG Tutorials 4: Why TestNG Is Called A Testing Framework? Uncategorized
August 2019 – Make Selenium Easy 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