-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_alphabet_or_digit.go
57 lines (49 loc) · 1.03 KB
/
is_alphabet_or_digit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
ASCII codes
0 : 48
9 : 57
A : 65
B : 66
Z : 90
a : 97
b : 98
z : 122
*/
package main
import (
"fmt"
"bufio"
"os"
)
func main(){
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter a character: ")
char, _, err := reader.ReadRune()
if err != nil {
fmt.Println(err)
}
if (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') {
fmt.Println("Given character is an alphabet")
} else if char >= '0' && char <= '9' {
fmt.Println("Given character is a digit")
}
/*
// Method 2: compare with ascii values
if (char >= 65 && char <= 122) || (char >= 97 && char <= 90) {
fmt.Println("Given character is an alphabet")
} else if char >= 48 && char <= 57 {
fmt.Println("Given character is a digit")
}
*/
}
/** Outputs
$ go run is_alphabet_or_digit.go
Enter a character: a
Given character is an alphabet
$ go run is_alphabet_or_digit.go
Enter a character: G
Given character is an alphabet
$ go run is_alphabet_or_digit.go
Enter a character: 5
Given character is a digit
*/