Wednesday 27 July 2016

Chapter 4 Exercise 22, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.





4.22 (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.

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

import java.util.Scanner;
 
public class ProgrammingEx4_22 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter string s1:");
  String s1 = input.nextLine();
  System.out.print("Enter string s2:");
  String s2 = input.nextLine();
 
  if (s1.contains(s2)) {
   System.out.println(s2 + " is a substring of " + s1);
  } else {
   System.out.println(s2 + " is not a substring of " + s1);
  }
 
 }
 
}

No comments :

Post a Comment