Please also post the run file. Thanks. Write a program that prompts the user to
ID: 3732394 • Letter: P
Question
Please also post the run file. Thanks.
Write a program that prompts the user to enter two strings and tests whether the second string is a substring of the first string. Suppose the neighboring characters in the string are distinct. (Don't use the indexof method in the string class.) Analyze the time complexity of your algorithm Your algorithm needs to be at least o(n) time SAMPLE RUN 1: Enter a string s1: Welcome to Java Enter a string s2: come matched at index 3 SAMPLE RUN 2 Enter a string s1: kelcome to lava ava Enter a string s2: Java matched at index 11 Class Name: Exercise22 03Explanation / Answer
Hi
Here is my code:
import java.util.*;
import java.lang.String;
public class CheckingSubstring2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a String: ");
String string1 = input.next();
System.out.print("Please enter a second String: ");
String string2 = input.next();
int matchedValue = isSubstring(string1, string2);
if (matchedValue >0) {
System.out.println("The first string is a substring of the second.");
System.out.println(matchedValue );
} else {
System.out.println("The first string is NOT a substring of the second.");
}
}
public static int isSubstring(String string1, String string2) {
char c;
char d;
int match;
for (int i = 0; i < string1.length(); i++) {
c = string1.charAt(i);
for (int j = 0; i < string2.length(); i++) {
d = string2.charAt(i);
if (c == d) {
match = i;
} else {
match = false;
}
}
}
return match;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.