• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / Program to count number of occurrences of word in a string|Selenium Tutorial

Program to count number of occurrences of word in a string|Selenium Tutorial

July 29, 2015 by Mukesh Otwani 10 Comments

While attending interviews you should be ready with some sample program which they might ask because they want to see how you are applying logics for a specific program.

Today I am covering very basic program which simply check count  number of occurrences of a word in a string.

Program to count  number of occurrences of word in a string

The scenario is- You will be getting a specific String i.e Selenium Webdriver Online Tutorial and you need to search for e character and number of occurrence as well.

Approach-

1- First accept one String from user and Store in String variable.

2- Then accept char which you want to search in String

3- Take counter variable which will increment if match found

4- Finally print the counter value.

 

JAVA Program to count  number of occurrences of word in a string

public class CountProgram 
{
public static void main(String[] args) 
{

// Create object of Scanner class to accept values
Scanner scan=new Scanner(System.in);

System.out.println("Please Enter the String here");

// It will accept String from user and will store in variable
String str1=scan.next();

System.out.println("enter the latter to count");

// It will accept char from user and will store in variable
String str2=scan.next();

// Now from Second String get the value using charAt method
char cha = str2.charAt(0);

// take count variable
int count=0;

// Run a for loop that will run based on String length
for(int i=0;i<str1.length()-1;i++)
{

// This will check if match found
if(str1.charAt(i)==cha)

{

// It will increment the counter
count++;

}

}

// Finally print the count
System.out.println(count);

}

}

 

For above program, there can be many solutions, I have posted which I use generally if you have any other solution then please let me know so that I can add in this post.

Guys be ready with such type of questions and I will try to post as much as possible.If you are looking for any specific program the comment below.

 

Filed Under: Basic Selenium

Reader Interactions

Comments

  1. Tahir says

    September 14, 2016 at 1:57 PM

    Thanks, Sir !!!
    Please post soon.

    Reply
    • Mukesh Otwani says

      September 15, 2016 at 6:07 PM

      Sure

      Reply
  2. Aamer Hussain says

    June 30, 2016 at 11:16 PM

    I am preparing for interviews for Selenium profile. Facing issues with Java questions asked in interview.
    Could you please help me with core java questions necessary for Selenium & also basic(sometimes tricky) java programs that are frequently asked in Selenium interviews. Thanksss a bunch again for your help!!

    Reply
    • Mukesh Otwani says

      July 2, 2016 at 2:59 PM

      Hey Aamer,

      I dont have Java question but I will prepare and will upload soon.

      Reply
  3. surabhi says

    June 17, 2016 at 11:29 AM

    hi Mukesh,

    Plz upload basic java programs which be asked in a selenium interview.

    Reply
    • Mukesh Otwani says

      June 20, 2016 at 12:10 PM

      Hi Surabhi,

      Sure will upload soon.

      Reply
  4. dinesh says

    May 20, 2016 at 8:55 PM

    Hi Mukesh,

    can you post all the basic java programs which is expected in the interviews.

    Reply
    • Mukesh Otwani says

      May 21, 2016 at 1:15 AM

      Hi Dinesh,

      yes will upload soon.

      Reply
      • usha says

        April 8, 2021 at 3:20 PM

        is it uploaded, please share link

        Reply
        • Mukesh Otwani says

          April 8, 2021 at 5:33 PM

          I’ll upload it soon

          Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?