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

Answer:

The blanks are filled below:


Filled Blanks

 

  public boolean recognize(String str)
  {
    final int reject = 3;
    int  state = 0;
    char current;

    while ( ... )
    {
      current = next character in the string;

      // State 0
      if      ( state==0 && current=='a' )
        state = 1;

      // State 1
      else if ( state==1  && current >= 'a' && current <= 'y')
        state = 1;

      else if ( state==1  && current == 'z')
        state = 2;

     // State 2
      else if ( state==2  && current >= 'a' && current <= 'y')
        state = 1;

      else if ( state==2  && current == 'z')
        state = 2;

      // no transition 
      else
        state = reject ;
 
    }

  }

We need to do something about the lines:

    while ( ... )
    {
      current = next character in the string;

The charAt(int index) method of class String returns the character at the given index.


QUESTION 11:

What is the index of the first character in a string?