Golang Program to check character is vowel or consonant


April 8, 2022, Learn eTutorial
1273

How to check character is vowel or consonant

The letters a, e, i, o, and u are vowels. All the other letters are consonants. To check wheher the given character is a vowel or consonant we have to compare the given character with the letters a, e, i, o, u. If checking returns a true value then it is a vowel.

How to check character is vowel or consonant in Go Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing how to check character is vowel or consonant in the Go language. Here variable chr is used to hold the read character. For reading the character create the reader using NewReader. Check the character read is a vowel or consonant by comparing the characters with vowels.

func (r *Reader) ReadByte() (byte, error) 

ReadByte implements the io.ByteReader interface

Finally print the results. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt, bufio, os

STEP 2: Start function main()

STEP 3: Create a reader using NewReader as reader := bufio.NewReader(os.Stdin)

STEP 4: Read the character into variable chr as chr, _, _ := reader.ReadRune()

STEP 5: Check the character is vowel or consonant by comparing the characters with vowels

STEP 6: Use if statement for printing proper message

 

Golang Source Code

                                          package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter the character = ")
    chr, _ := reader.ReadByte()

    if chr == 'a' || chr == 'e' || chr == 'i' || chr == 'o' || chr == 'u' ||
        chr == 'A' || chr == 'E' || chr == 'I' || chr == 'O' || chr == 'U' {
        fmt.Printf("%c is a VOWEL character\n", chr)
    } else {
        fmt.Printf("%c is a CONSONANT\n", chr)
    }
}
                                      

OUTPUT

Enter the character = e
e is a VOWEL character

Enter the character = c
c is a CONSONANT