Selective Copying of Characters

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Yes, since our definition of palindrome ignores case. One or the other of these two methods could be used.

Selective Copying of Characters

Another possibility is to use the String method equalsIgnoreCase(). But it is probably easiest to ignore case from the start. Here is the method:

class Tester
{
  public boolean test( String trial )
  {
    String lower = trial.toLowerCase();

    StringBuffer azBuffer  = new StringBuffer();

    for ( int j=0; j < ; j++ )
    {
       
       // convert all characters to lower case, then copy only
       // 'a' thru 'a' to the buffer
       char c = lower.charAt(j);
       if ( c >= 'a' && c <= 'z' )
         azBuffer.( c );
    }
    . . . .

  }
}

public class PalindromeTester
{
  . . . . .
}

We will soon use the reverse() method of StringBuffer. But first we need to copy only the characters 'a' through 'z' into azBuffer. This is how the method ignores spaces and punctuation.

QUESTION 10:

Fill in the blanks with the appropriate methods from class String and StringBuffer. (Look at the list of methods a few pages back.)