Scala program to check the given character is vowel and consonant

Here, we are going to learn how to check the given character is vowel and consonant in Scala programming language?
Submitted by Nidhi, on April 28, 2021 [Last updated : March 10, 2023]

Scala – Check Vowel or Constant using Match Statement

Here, we will read a character from the user and check given character is vowel or consonant.

Scala code to read a weekday number and print weekday name using match case

The source code to check the given character is vowel and consonant is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to check the given character 
// is vowel and consonant

object Sample {
    def main(args: Array[String]) {  
        var ch:Char=0;
    
        print("Enter character: ")  
        ch=scala.io.StdIn.readChar()
        
        ch match{
            case 'A'=>printf("%c is a VOWEL.\n",ch);
            case 'E'=>printf("%c is a VOWEL.\n",ch);
            case 'I'=>printf("%c is a VOWEL.\n",ch);
            case 'O'=>printf("%c is a VOWEL.\n",ch);
            case 'U'=>printf("%c is a VOWEL.\n",ch);
            case 'a'=>printf("%c is a VOWEL.\n",ch);
            case 'e'=>printf("%c is a VOWEL.\n",ch);
            case 'i'=>printf("%c is a VOWEL.\n",ch);
            case 'o'=>printf("%c is a VOWEL.\n",ch);
            case 'u'=>printf("%c is a VOWEL.\n",ch);
            case _=>printf("%c is a CONSONANT.\n",ch);
        }  
    }
}  

Output

Enter character: E
E is a VOWEL.

Explanation

In the above program, we used an object-oriented approach to create the program. Here, we created an object Sample. We defined main() function. The main() function is the entry point for the program.

In the main() function, we created a  character variable ch initialized with 0. Then we read the value of ch from the user. Here, we used the match case to check vowels. If the given character is not matched with any cases. Then it will be treated as a consonant.

Scala Pattern Matching Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.