package hw5; import java.util.Scanner; public class Substring { // Function looking for a substring in a string. // If found, it returns the index of the substring. // If not found, it returns -1. static public int isSubstring(String subs, String text) { int m = subs.length(), n = text.length(); int i, j; boolean found; for (int i = 0; i <= n; i++) { found = true; for (int j = 0; j <= m; j++) if (subs.charAt(j) != text.charAt(i+j)) { found = false; break; } if (found) return i; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a string"); String s1 = scan.nextLine(); System.out.println("Enter a possible substring"); String s2 = scan.nextLine(); if (isSubstring(s2, s1)) System.out.println("The substring was found"); else System.out.println("The substring was not found"); } }