Scala Program to Check Whether an Alphabet is Vowel or Consonant

Posted in

Sangeeta Gulia
Last updated on January 13, 2023

    In the previous blog, we helped you write a Scala program to check whether a number is even or odd. Like even and odd numbers, vowels and consonants are other basic concepts that you learned in childhood.

    Vowel: The letters ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ are vowels.

    Consonant: All other alphabets other than vowels are referred to as consonants

    This article will help you know how to write a Scala program to check whether an alphabet is a vowel or a consonant.

    Scala Program to Check Whether an Alphabet is Vowel or Consonant

    We can write a Scala program that identifies whether an alphabet is a vowel or consonant using two different methods:

    • If-else Ladder
    • Pattern Matching

    Let us discuss both of these methods below.

    1. If-else Ladder

    Program

    objectVowelOrConsonantextendsApp {
    
    println("VowelOrConsonent: Please enter a character")
    val inputCharacter = scala.io.StdIn.readChar()
    
    if (inputCharacter == 'a' || inputCharacter == 'e' || inputCharacter == 'i' || inputCharacter == 'o' || inputCharacter == 'u') {
    println("Entered character is a vowel")
    } elseif (inputCharacter == 'A' || inputCharacter == 'E' || inputCharacter == 'I' || inputCharacter == 'O' || inputCharacter == 'U') {
    println("Entered character is a vowel")
    } else {
    println("Entered character is a consonant")
    }
    }

    Output

    Sample Output 1:

    VowelOrConsonent: Please enter a character
    A
    Entered character is a vowel

    Sample Output 2:

    VowelOrConsonent: Please enter a character
    W
    Entered character is a consonant

    Sample Output 3:

    VowelOrConsonent: Please enter a character
    h
    Entered character is a consonant

    Explanation

    In the above program, we wrote the first line as an instruction to the user so that the user is aware that they need to enter a character. Using the readChar() function, we read a character from the console and stored it in the inputCharacter variable.

    Later, we performed a comparison check on the input character to determine whether it was a vowel or a consonant. One important thing to note here is we should perform a check on both the lowercase and uppercase characters as I did in the two initial conditions present in the if expression.

    Note: You can also use the toLower() method to convert the entered character to lowercase, and then you will not require the second if check to handle capital case scenario .

    Program

    objectVowelOrConsonantextendsApp {
    
    println("VowelOrConsonent: Please enter a character")
    val inputCharacter = scala.io.StdIn.readChar().toLower
    
    if (inputCharacter == 'a' || inputCharacter == 'e' || inputCharacter == 'i' || inputCharacter == 'o' || inputCharacter == 'u') {
    println("Entered character is a vowel")
    } else {
    println("Entered character is a consonant")
    }
    
    }

    Output:

    VowelOrConsonent: Please enter a character
    T
    Entered character is a consonant

    2. Pattern matching

    In this method, we will be using the guard pattern type to identify whether a character is a vowel or consonant.

    Program

    objectVowelOrConsonantextendsApp {
    
    println("VowelOrConsonent: Please enter a character")
    val inputCharacter = scala.io.StdIn.readChar()
    
    inputCharacter.toLower match {
    case data if data == 'a' || data == 'e' || data == 'i' || data == 'o' || data == 'u' => println("Entered character is a vowel")
    case _ => println("Entered character is a consonant")
    }
    }

    Output

    VowelOrConsonent: Please enter a character
    A
    Entered character is a vowel

    Explanation

    In the above program, we first took character input from the user. To cover both uppercase and lowercase scenarios, we converted the input character to lowercase. Doing this will require only one check condition that serves both uppercase and lowercase characters.

    The first case statement inside the pattern match construct checks for the characters that are referred to as vowels. If the input character does not match any of those characters, it will consider the input character as a consonant.

    Please note we have used ‘ || (OR operator) ’ in the first case construct, which will match if any of the conditions out of 5 conditions returns true.

    Conclusion

    Using the if-else construct and pattern matching, you can write a Scala program to check whether an alphabet is a vowel or consonant. Implement both these methods and try out entering different inputs to understand the output.

    Keep Coding, Keep Exploring!



    Leave a Comment on this Post

    0 Comments