go to previous page   go to home page   go to next page

Answer:

  1. If StringA has no characters and StringB has no characters, then the strings ARE equal.
  2. If StringA has no characters and StringB has some characters, then the strings are NOT equal.
  3. If StringA has some characters and StringB has no characters, then the strings are NOT equal.
  4. Otherwise,
    • convert the first character of each string to lower case.
    • if the first characters are different, then the strings are NOT equal.
    • if the first characters are the same, then the strings ARE equal if the tails are equal.

Implementation of equalsIC

The implementation uses the static method toLowerCase(char ch) of the wrapper class Character, which returns a lower case version of its char argument.

public class EqualsICTester
{
  public static boolean equalsIC( String strA, String strB )
  {
    if ( strA.isEmpty() && strB.isEmpty() ) 
      return true;
    else if ( strA.isEmpty() || strB.isEmpty() ) 
      return false;
    else if ( Character.toLowerCase( strA.charAt(0) ) != Character.toLowerCase( strB.charAt(0) ) )
      return false;
    else
      return equalsIC( strA.substring(1), strB.substring(1));
  }
   
  public static void main (String[] args)
  {
    String strA = "Applecart";
    String strB = "appleCarT";
    
    if ( equalsIC( strA, strB ) )
      System.out.println(  "\"" + strA + "\" == \"" + strB  + "\"");
    else
      System.out.println(  "\"" + strA + "\" != \"" + strB  + "\"");
    
  }
}


QUESTION 24:

Would it work to use the toLowerCase() method of class String to convert both strings to lower case, and then just use our equals method?


go to previous page   go to home page   go to next page