Basic Selenium

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

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.

 

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

10 thoughts on “Program to count number of occurrences of word in a string|Selenium Tutorial

  1. Tahir says:

    Thanks, Sir !!!
    Please post soon.

  2. Aamer Hussain says:

    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!!

    1. Hey Aamer,

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

  3. surabhi says:

    hi Mukesh,

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

    1. Hi Surabhi,

      Sure will upload soon.

  4. dinesh says:

    Hi Mukesh,

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

    1. Hi Dinesh,

      yes will upload soon.

      1. usha says:

        is it uploaded, please share link

        1. I’ll upload it soon

Leave a 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.