// Exercise7_4.java: Check whether the first string is a substring
// of the second string
public class Exercise7_4
{
  /**Main method*/
  public static void main(String[] args)
  {
    // Prompt the user to enter two strings
    System.out.print("Enter the first string: ");
    String first = MyInput.readString();

    System.out.print("Enter the second string: ");
    String second = MyInput.readString();

    if (isSubstring(first, second))
    {
      System.out.println(first + " is a substring of " + second);
    }
    else
    {
      System.out.println(first + " is not a substring of " + second);
    }
  }

  /**Check if the first string is a substring of the second string*/
  public static boolean isSubstring(String first, String second)
  {
    int remainingLength = second.length();
    int startingIndex = 0;

    // Note toWhile is a label. You can use break with a label
    // attached.
    toWhile: while (first.length() <= remainingLength)
    {
      // What is wrong if the following line is used
      // for (int i=startingIndex; i<=first.length(); i++)
      for (int i=0; i<first.length(); i++)
      {
        if (first.charAt(i) != second.charAt(startingIndex+i))
        {
          startingIndex++;
          remainingLength--;
          continue toWhile;
        }
      }

      return true;
    }

    return false;
  }
}
