check alphabet or not in java with 3 different approaches | CY

How to check whether a character is alphabet or not using java programming language with 3 different approaches. codeYourslf java beginner

alphabet or not in java programming

check alphabet or not in java with 3 different approaches | CY

In this article you can learn how to check whether a given character is alphabet or not with 3 different approaches in java.

3 different approaches:-

- I have provided the source code and also explained in detail.

Approach :- 1 "using if else statement"

package CodeYourslf.allPrograms.checkAlpabet;

import java.util.Scanner;

public class usingIfElse {

    public static void main(String []args){

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter the character: ");

      char ch = sc.next().charAt(0);

      

      if(ch >= 'a' && ch <= 'z'){

        System.out.println(ch + " is an alphabet.");

      }

      else if(ch >= 'A' && ch <= 'Z'){

        System.out.println(ch + " is an alphabet.");

      }

      else{

        System.out.println(ch + " is not an alphabet. ");

      }

    }

}

Output

check alphabet or not in java with 3 different approaches | CY

Approach :- 2 "using ternary operator"

package CodeYourslf.allPrograms.checkAlpabet;

import java.util.Scanner;

public class usingTernary {

    public static void main(String[] args){

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter the character: ");

      char ch = sc.next().charAt(0);

      ch = Character.toLowerCase(ch);

      //System.out.println(ch);

      

      String output = (ch >= 'a' && ch <= 'z')?"Alphabet":"Not alphabet";

      System.out.println(output);

    }

}

Approach :- 3 "using inbuilt method"

package CodeYourslf.allPrograms.checkAlpabet;

import java.util.Scanner;

public class usingMethod {

    public static void main(String []args){

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter the alphabet: ");

      char ch = sc.next().charAt(0);

      

      boolean result = Character.isAlphabetic(ch);

      if(result == true){

        System.out.println("Alphabet");

      }

      else{

        System.out.println("Not alphabet");

      }

    }

}

Details Explanation for how to check alphabet or not in Java

- Here you get proper explanation to all the approaches those are mentioned above.

Approach :-1

- Here first I have received a character in ch variable. Then using if condition I have checked ch is greater than 'a' and less than 'z'. This is for small letters.

- Now in else if condition, I have checked for capital letters. If both condition will satisfied it will print "character is alphabet". Otherwise not

Approach :- 2

- Here I have used ternary operator to check the condition.

- You can see in approach 2 I haven't checked for capital letters, because by the help of Character.toLowerCase(ch) method, the entered capital letters will be converted into small.

- and finally using ternary operator, we can easily you can find a character is alphabet or not in java.

Approach :- 3

- Here I have used the java inbuilt method i.e Character.isAlphabetic(ch) to check alphabet or not.

- If the character is an alphabet, it will return true. Here I have store that on result variable.

- finally we can check if the result is equal to true it will print alphabet, otherwise not an alphabet. 

Also read :- 

Hey if you learn anything new from this article, then please share with your friends.

If you have any query or any doubt, feel free to share in the comments.

`

3 comments

  1. Please comment here
  2. It's nice ..
  3. You are great .. Carry-on bro
Hey Please 👏, feel free to share your opinion 🌍
© codeYourslf. All rights reserved. Distributed by Pixabin