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

Answer:

In the above, what character is the first one removed from the string? A

What character is the last one appended to the reversed string? A


Java Implementation

public class ReverseTester
{
  public static String reverse( String str )
  {
    if ( str.isEmpty() )       // use str.equals("") for older version of Java 
      return "";
    else 
      return reverse( str.substring(1) ) + str.charAt(0) ;
  }
   
  public static void main (String[] args)
  {
    String strA = "Applecart";  
    System.out.println(  "Reverse of \"" + strA + " is \"" + reverse( strA ) + "\"");
  }
}

reverse( str.substring(1) ) is the reverse of the tail.

reverse( str.substring(1) ) + str.charAt(0) appends the first character of the argument to the end of the reversed tail.


QUESTION 14:

Is the character A equal to the character A?

Is the string "pplecart" equal to the string "pplecart"?

Is the string "Applecart" equal to the string "Applecart"?