Lab 13: Week of Nov 14-21
 


Question 1: Write a method
public
static int find(String s, String t)
That tests whether the string t is contained in string s. If so, it returns the offset of the first match. If not, it returns -1. For example: 

find(“Mississippi”, “is”) returns 1

find(“Mississippi”, “Miss”) returns 0

find(“Mississippi”, “pi”) returns 9

find(“Mississippi”, “hip”) returns  -1 

Hint: If t is longer than s then you can return -1. Otherwise, compare t with the initial substring of s with t.length() characters. If those are the same strings then return 0. Otherwise call the method recursively with the remaining of s (that is, s without the first character) 

Question 2: A palindrome is a string that is identical to its reverse, ignoring uppercase, lowercase, spaces and punctuation marks. Examples are “Radar”, and “A man, a plan, a canal, Panama”. Write a predicate method: 

public static Boolean isPalindrome(String s) 

Hint: If the first character of the string is not a letter then just ignore it. Do the same for the last letter. If both the first and the last characters are letters, check whether they match. If they don’t then the string is not a palindrome. If they do then remove the first and last characters and call the method again.