Question 4.6:Write a Java program to check whether a given substring is present in the given string.
Also find the number of times a given word 'search' appears in the given string.
Test Data:
Input the string : This is a test string.
Input the substring to be search : test
Expected Output:
Result: The substring is exist in the string.
test has been found 1 time.
Input the string : This is a test string. I am in love with this string.
Input the substring to be search : string
Expected Output:
Result: The substring is exist in the string.
string has been found 2 times.
Input the string : This is a test string. I am in love with this string.
Input the substring to be search : no
Expected Output:
Result: The substring is not exist in the string.
no has not been found yet.
Solution:
package lab4problem6;
import java.util.Scanner;
public class Lab4Problem6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input the string : ");
String string = input.nextLine();
System.out.print("Input the substring to be search : ");
String subString = input.nextLine();
int stringLength = string.length();
int subStringLength = subString.length();
int count = 0,b=0;
for (int i = 0; i < stringLength; i++) {
for (int j = 0; j < subStringLength; j++) {
if (string.charAt(i)==subString.charAt(j)) {
count++;
}
if (string.charAt(i)!=subString.charAt(j)) {
i=i-j;
count = 0;
break;
}
if (count == subStringLength) {
b = b + 1;
count = 0;
}
i++;
}
}
if (b != 0) {
System.out.println("Result: The substring is exist in the string.\n" + subString +
" has been found " + b + " time.");
} else {
System.out.println("Result: The substring is not exist in the string.\n" + subString +
" has not been found yet.");
}
}
}