The base cases are:
1. equals( "", "" ) = true 2. equals( "", X ) = false if X is not the empty string 3. equals( X, "" ) = false if X is not the empty string 4. equals( x+X, y+Y ) = false if x != y
These are base cases because the result can be computed immediately.
Of course there is an equals()
method that is part of the
class String
.
Let us forget this and write our own method.
This should be merely a matter of translation:
boolean equals( String strA, String strB ) { // 1. equals( "", "" ) = true if ( strA.length() ________ 0 && strB.length() ________ 0 ) return true; // 2. equals( "", X ) = false if X is not the empty string else if ( strA.length() ________ 0 && strB.length() ________ 0 ) return false; // 3. equals( X, "" ) = false if X is not the empty string else if ( strA.length() ________ 0 && strB.length() ________ 0 ) return false; // 4. equals( x+X, y+Y ) = false if x != y else if ( strA.charAt(0) ________ strB.charAt(0) ) return false; // 5. equals( x+X, y+Y ) = true if x == y and equals( X, Y ) else return ________( strA.substring(1), strB.substring(1) ); }
To detect an empty string, check if the length is zero.
Remember that an empty string is a string that contains no characters.
The value null
is something different.