I am trying to find the missing letter in a string of consecutive letters in Java

738 views Asked by At

I don't understand where is the mistake. Can you tell me where is the mistake?

public class FindtheMissingLetter {
    public static char findMissingLetter(char[] array)
      {
        char letter;
        for(int i=0;i<array.length;i++)
        {
            if(array[i]==array[i-1]+2)
            {
                letter=(char) (array[i-1]+1);
                return letter;

            }
        }
        return ' ';
      }

}
1

There are 1 answers

0
CallSign-Filter On

You need to think about the logic and work out the math for each pass of the loop. Look at the line:

if(array[i]==array[i-1]+2)

and think about what the value would be for each iteration of your loop.

The best way to do this is to create a small char[] (say 3 letters) and do it by hand using your algorithm. If you do that, you should easily see your error.