The task is to create a function that takes two strings as arguments and returns true if the second string is a suffix of the first. My code worked for all test cases except two. I can't figure out where I'm going wrong.
Examples:
isSuffix("vocation", "-logy") ➞ false
isSuffix("arachnophobia", "-phobia") ➞ true
My code:
public static boolean isSuffix(String word, String suffix) {
String suff = "";
for(int i = 1; i < suffix.length(); i++) suff += "" + suffix.charAt(i);
for(int i = 0; i < suff.length(); i++){
if(!(word.charAt(suff.length()-1-i) == suff.charAt(suff.length()-1-i))) return false;
}
return true;
}
The test cases I'm failing:
@Test
public void test5() {
assertThat(Program.isSuffix("arachnophobia", "-phobia"), is(true));
}
@Test
public void test6() {
assertThat(Program.isSuffix("rhinoplasty", "-plasty"), is(true));
}