Golang Program to check the character is an alphabet


February 19, 2022, Learn eTutorial
1131

How to check the character is an alphabet or not

Computer stores ASCII value of characters (number between 0 and 127) rather than the character itself. The ASCII value of lower case alphabets are from 97 to 122 and the ASCII value of uppercase alphabets are from 65 to 90. When we compare variable ‘a’ to ‘z’ and ‘A’ to ‘Z’, the variable is compared with the ASCII value of the alphabets 97 to 122 and 65 to 90 respectively. Some languages will provide built in functions to check the given character is an alphabet or not.

How to check the character is an alphabet in G Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing how to check the character is an alphabet in the Go language. Here variable chr is used to hold the read character. For reading the character create the reader using NewReader.

IsLetter reports whether the given rune is a letter (category L)

Check the character read is an alphabet or not by using unicode.IsLetter(chr). 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, unicode

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 an alphabet or not by using unicode.IsLetter(chr)

STEP 6: Use if statement for printing proper message

Golang Source Code

                                          package main

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

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter any character = ")
    chr, _, _ := reader.ReadRune()

    if unicode.IsLetter(chr) {
        fmt.Printf("%c is an Alphabet\n", chr)
    } else {
        fmt.Printf("%c is not a Alphabet\n", chr)
    }
}
                                      

OUTPUT

Enter any character = H
H is an Alphabet

Enter any character = 5
5 is not an Alphabet