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.
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
System.out.println("enter the latter to count");
// It will accept char from user and will store in variable
// Now from Second String get the value using charAt method
char cha = str2.charAt(0);
// take count variable
// Run a for loop that will run based on String length
// This will check if match found
// It will increment the counter
// Finally print the 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.