Q:

(Check substring) Write a program that prompts the user to enter two strings and reports whether the second string is a substring of the first string

0

(Check substring) Write a program that prompts the user to enter two strings and reports whether the second string is a substring of the first string.

Output:

Enter string s1: ABCD
Enter string s2: BC
BC is a substring of ABCD

Enter string s1: ABCD
Enter string s2: BDC
BDC is not a substring of ABCD

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*
(Check substring) Write a program that prompts the user to enter two strings and
reports whether the second string is a substring of the first string.
*/
import java.util.Scanner;

public class Exercise_04_22 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		// Prompt the user to enter two strings
		System.out.print("Enter string s1: ");
		String string1 = input.nextLine();
		System.out.print("Enter string s2: ");
		String string2 = input.nextLine();

		// Display wheater the second string
		// is a substring of the frist string
		System.out.println(
			string2 + ((string1.contains(string2)) ? " is " : " is not ") + 
			"a substring of " + string1);
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now