I'm writing with JAVA, and I wrote a recursive boolean function that gets 2 strings and is supposed to return if the first one is prefix of the second one. My code:
public static boolean prefix(String s, String t)
{
int i = s.length()-1;
if (s.charAt(i) == t.charAt(i) && s.length() >= 0)
{
return true;
}
return false;
}
If I'm writing in the main for example:
s = "Del";
f = "Delight";
The function is working as well.
But if I'm writing s = "Dellll", f = "Dell", it says "Out of bounds". Why?
The second thing is that it's not working with big and small characters. For example:
s ="Dell"
f ="dell"
It will return true for the above.
Thank you.
s.length()-1to get the last character ofsandthowevertmay be shorter, in which case it will throwIndexOutOfBoundsException