What does "Xtail".charAt(0) evaluate to? X — the head of the string.
What does "Xtail".substring(1) evaluate to? tail — the tail of the string.
public class EqualsTester
{
public static boolean myEquals( String strA, String strB )
{
if ( strA.isEmpty() && strB.isEmpty() )
return true;
else if ( strA.isEmpty() || strB.isEmpty() )
return false;
else if ( strA.charAt(0) != strB.charAt(0) )
return false;
else
return myEquals( strA.substring(1), strB.substring(1));
}
public static void main (String[] args)
{
String strA = "applecart";
String strB = "appleCart";
if ( myEquals( strA, strB ) )
System.out.println( "\"" + strA + "\" equals \"" + strB + "\"");
else
System.out.println( "\"" + strA + "\" not equals \"" + strB + "\"");
}
}
Java Detail: "\"" is the string consisting of one " (the backslash
is needed to escape that character).
Here is the first part of the recursive definition:
Does this part of the code correctly implement that rule:
if ( strA.isEmpty() && strB.isEmpty() )
return true;