Testing a letter for a vowel

vowelThe letters A, E, I, O and U are referred to as vowels in the English language. In this post I would be discussing some methods in Java for testing a letter for a vowel. The most important thing is that there are 10 vowels from computing perspective! These are A, E, I, O, U, a, e, i, o and u. This distinction is important because students usually overlook this perspective when writing programs. The simplest Java program to test any given letter for a vowel is as follows:

class Vowel{
    public static boolean isVowel( char ch ){
        if( ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' ||
        ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
            return true;
        else 
            return false;
    }
    public static void main( String []args ){
        System.out.println("A :" + isVowel( 'A' ) );
        System.out.println("E :" + isVowel( 'E' ) );
        System.out.println("I :" + isVowel( 'I' ) );
        System.out.println("O :" + isVowel( 'O' ) );
        System.out.println("U :" + isVowel( 'U' ) );
        System.out.println("B :" + isVowel( 'B' ) );
        System.out.println("Z :" + isVowel( 'Z' ) );
        System.out.println("a :" + isVowel( 'a' ) );
        System.out.println("e :" + isVowel( 'e' ) );
        System.out.println("i :" + isVowel( 'i' ) );
        System.out.println("o :" + isVowel( 'o' ) );
        System.out.println("u :" + isVowel( 'u' ) );
        System.out.println("b :" + isVowel( 'b' ) );
        System.out.println("z :" + isVowel( 'z' ) );

    }
}

The logic is very straight forward and the important lines in the above program for test for a vowel are line number 3-4. We are checking whether the given character is a vowel or not by explicitly testing the argument against all the vowels. The repetitive typing can be avoided by converting all character into uppercase or lowercase as follows:

class Vowel{
    public static boolean isVowel( char ch ){
        ch = Character.toUpperCase( ch );
        if( ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' )
            return true;
        else 
            return false;
    }
    public static void main( String []args ){
        System.out.println("A :" + isVowel( 'A' ) );
        System.out.println("E :" + isVowel( 'E' ) );
        System.out.println("I :" + isVowel( 'I' ) );
        System.out.println("O :" + isVowel( 'O' ) );
        System.out.println("U :" + isVowel( 'U' ) );
        System.out.println("B :" + isVowel( 'B' ) );
        System.out.println("Z :" + isVowel( 'Z' ) );
        System.out.println("a :" + isVowel( 'a' ) );
        System.out.println("e :" + isVowel( 'e' ) );
        System.out.println("i :" + isVowel( 'i' ) );
        System.out.println("o :" + isVowel( 'o' ) );
        System.out.println("u :" + isVowel( 'u' ) );
        System.out.println("b :" + isVowel( 'b' ) );
        System.out.println("z :" + isVowel( 'z' ) );
    }
}

The same can be done using switch case as follows:

class Vowel{
    public static boolean isVowel( char ch ){
        ch = Character.toUpperCase( ch );
        switch( ch ){
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                return true;
            default:
                return false;
        }
    }
    public static void main( String []args ){
        System.out.println("A :" + isVowel( 'A' ) );
        System.out.println("E :" + isVowel( 'E' ) );
        System.out.println("I :" + isVowel( 'I' ) );
        System.out.println("O :" + isVowel( 'O' ) );
        System.out.println("U :" + isVowel( 'U' ) );
        System.out.println("B :" + isVowel( 'B' ) ); highlight:[3,4]
        System.out.println("Z :" + isVowel( 'Z' ) );
        System.out.println("a :" + isVowel( 'a' ) );
        System.out.println("e :" + isVowel( 'e' ) );
        System.out.println("i :" + isVowel( 'i' ) );
        System.out.println("o :" + isVowel( 'o' ) );
        System.out.println("u :" + isVowel( 'u' ) );
        System.out.println("b :" + isVowel( 'b' ) );
        System.out.println("z :" + isVowel( 'z' ) );
    }
}

The above program requires a lot of repititive typing. Let us see a method which requires substantially less typing work. There is a function indexOf( ) which returns the index within the string of the first occurrence of the specified character. It returns -1 if the character passed as an argument doesn’t exist in the string. If the value returned is zero or more it indicates the presence of the character passed as argument and if the value returned is negative one it indicates the absence of the character passed as an argument. This concept can also be used to test a letter for a vowel using very compact code as follows:

class Vowel{
    public static boolean isVowel( char ch ){
        return (new String("AEIOUaeiou")).indexOf(ch) >= 0 ;
    }
    public static void main( String []args ){
        System.out.println("A :" + isVowel( 'A' ) );
        System.out.println("E :" + isVowel( 'E' ) );
        System.out.println("I :" + isVowel( 'I' ) );
        System.out.println("O :" + isVowel( 'O' ) );
        System.out.println("U :" + isVowel( 'U' ) );
        System.out.println("B :" + isVowel( 'B' ) );
        System.out.println("Z :" + isVowel( 'Z' ) );
        System.out.println("a :" + isVowel( 'a' ) );
        System.out.println("e :" + isVowel( 'e' ) );
        System.out.println("i :" + isVowel( 'i' ) );
        System.out.println("o :" + isVowel( 'o' ) );
        System.out.println("u :" + isVowel( 'u' ) );
        System.out.println("b :" + isVowel( 'b' ) );
        System.out.println("z :" + isVowel( 'z' ) );

    }
}

In the above program line number 3 is important. On this line we are first creating a string (anonymous object) using the parametrized constructor. We are initializing the string with “AEIOUaeiou”, that is with all the vowels. Then, we are checking if the character passed as argument exist in the string or not. If zero or more is returned we are returning true else we are returning false. There is no need for taking a reference of the string because we won’t be requiring it again. As usual, the main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.