- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check whether a character is in the Alphabet or not in Golang?
In this tutorial, we will learn how to check whether the character is an alphabet or not. This tutorial includes two ways to achieve this −
First, using the built-in function isLetter() present in the fmt library will reduce the line of codes.
Another way is to use the concept of ASCII values as every character has a unique ASCII value using which we can find out whether the current character is an alphabet or not.
The ranges of uppercase and lowercase alphabets are as follows −
uppercase alphabets – 65 to 90
lowercase alphabets – 97 to 122
If the ASCII value of the character lies between the above ranges then the character is an alphabet.
Method 1: Using isLetter(0 function
In this example, we will use the built-in function isLetter() present in the fmt library to check whether the character is an alphabet.
Syntax
The isLetter() present in fmt library syntax is below. This function accept a character as argument.
Func isLetter(r rune) bool
Algorithm
Step 1 − Initialize the string of characters.
Step 2 − Run a for loop over the string.
Step 3 − Start an if condition and call IsLetter() function in the if condition.
Step 4 − Print the result accordingly.
Example
package main import ( // fmt package provides the function to print anything "fmt" // unicode function is providing isLetter function "unicode" ) func main() { // declaring and initializing the variable using the shorthand method in Golang characters := "A&j()K" fmt.Println("Golang program to check the character is an alphabet or not using IsLetter() function present in the Unicode library.") // running a for loop to check each character in the string is alphabet or not for i := 0; i < len(characters); i++ { // calling the Isletter() function and printing the result on the basis // of return value of the function if unicode.IsLetter(rune(characters[i])) { fmt.Printf("%c is a character.\n", characters[i]) } else { fmt.Printf("%c is not a character.\n", characters[i]) } } }
Output
% go run tutorialpoint.go Golang program to check whether the character is an alphabet or not using the IsLetter() function present in the Unicode library. A is a character. & is not a character. j is a character. ( is not a character. ) is not a character. K is a character.
Method 2:Using ASCII characters
In this example, we will use the ASCII characters range to check whether the character is an alphabet or not.
Syntax
The comparison will be make using the range of ASCII value of lower and uppercase alphabets as shown in below syntax.
if (characters[i] >= 65 && characters[i] <= 90) || (characters[i] >= 97 && characters[i] <= 122) { }
Algorithm
Step 1 − Initialize the string of characters.
Step 2 − Run a for loop over the string.
Step 3 − Start an if condition and compare the ASCII value of the current index character if it lies between 65 and 90 or 97 and 122.
Step 4 − Print the result accordingly.
Example
package main import ( // fmt package provides the function to print anything "fmt" ) func main() { // declaring and initializing the variable using the shorthand method in Golang characters := "A&j()K" fmt.Println("Golang program to check whether the character is an alphabet or not using the concept of ASCII values.") // running a for loop to check if each character in the string is alphabet or not for i := 0; i < len(characters); i++ { // checking that the ASCII value of the character is in between the range // of uppercase or lowercase characters or not if (characters[i] >= 65 && characters[i] <= 90) || (characters[i] >= 97 && characters[i] <= 122) { fmt.Printf("%c is a character.\n", characters[i]) } else { fmt.Printf("%c is not a character.\n", characters[i]) } } }
Output
% go run tutorialpoint.go Golang program to check whether the character is an alphabet or not using the concept of ASCII values. A is a character. & is not a character. j is a character. ( is not a character. ) is not a character. K is a character.
Conclusion
These are the two ways to check whether the character is an alphabet or not. The first way is more suitable in terms of modularity and code reusability. To learn more about go you can explore these tutorials.
- Related Articles
- Java Program to Check Whether a Character is Alphabet or Not
- Haskell Program to Check Whether a Character is Alphabet or Not
- C++ Program to Check Whether a Character is Alphabet or Not
- Golang Program to Check Whether an Alphabet is Vowel or Consonant
- Java program to find whether the given character is an alphabet or not
- Check whether a character is Lowercase or not in Java
- Check whether a character is Uppercase or not in Java
- Check If the Rune is a Space Character or not in Golang
- Check If the Rune is a Symbolic Character or not in Golang
- Check input character is alphabet, digit or special character in C
- Check If the Rune is a Unicode Punctuation Character or not in Golang
- Check whether the Average Character of the String is present or not in Python
- How to check whether a number is Positive or Negative in Golang?
- Java Program to Check Whether an Alphabet is Vowel or Consonant
- Haskell Program to Check Whether an Alphabet is Vowel or Consonant
