Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaCheck if a String Contains Only Alphabets in Java using ASCII Values

Check if a String Contains Only Alphabets in Java using ASCII Values

Given a string, now as of we all know that the task is to check whether a string contains only alphabets. Now we will be iterating character by character and checking the corresponding ASCII value attached to it. If not found means there is some other character other than “a-z” or “A-Z”. If we traverse the whole string and land up on finding all characters in a string within this domain pool then the string is surely only alphabetic.

Illustrations: 

Input  : Lazyroar
Output : True
 
Input  : Geeks4Geeks
Output : False
 
Input  : null
Output : False

In this article, the characters of the string are checked one by one using their ASCII values.

Algorithm: 

  1. Get the string
  2. Match the string: 
    • Check if the string is empty or not. If empty, return false
    • Check if the string is null or not. If null, return false.
    • If the string is neither empty nor null, then check the string characters one by one for alphabet using ASCII values.
  3. Return true if matched

Example 1:

Java




// Java Program to Check if a String Contains Only Alphabets
// Using ASCII values
 
// Importing required classes
import java.util.*;
 
// Class 1
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating object of helper class
        Helper h = new Helper();
 
        // Print statement
        System.out.println(
            h.isStringOnlyAlphabet("neveropen"));
    }
}
 
// Class 2
// Helper class
class Helper {
 
    public static boolean isStringOnlyAlphabet(String str)
    {
 
        // If string is empty or null
        if (str == null || str.equals("")) {
 
            // Return false
            return false;
        }
 
        // If we reach here we have character/s in string
        for (int i = 0; i < str.length(); i++) {
 
            // Getting character at indices
            // using charAt() method
            char ch = str.charAt(i);
            if ((!(ch >= 'A' && ch <= 'Z'))
                && (!(ch >= 'a' && ch <= 'z'))) {
                return false;
            }
        }
 
        // String is only alphabetic
        return true;
    }
}


Output

true

Complexity Analysis:

  • Time Complexity: O(N), where N is the length of the string.
  • Auxiliary Space: O(1), no extra space required so it is a constant.

Example 2:

Java




// Java program to check if String contains only Alphabets
// Using ASCII Values
 
// Main class
class GFG {
 
    // Method 1
    // To check String for only Alphabets
    public static boolean isStringOnlyAlphabet(String str)
    {
 
        // If there is no string
        if (str == null || str.equals("")) {
            return false;
        }
 
        // Iterating as now we encounter string
        // using length() method
        for (int i = 0; i < str.length(); i++) {
 
            char ch = str.charAt(i);
 
            // Checking for any other aphabetic character
            // present in string
            if ((!(ch >= 'A' && ch <= 'Z'))
                && (!(ch >= 'a' && ch <= 'z'))) {
 
                // Then string is not only alphabetic
                return false;
            }
        }
 
        // If we reach here, it means
        // string is only aphabetic
        return true;
    }
 
    // Method 2
    // Main method
    public static void main(String[] args)
    {
 
        // Checking for True case
        System.out.println("Test Case 1:");
 
        String str1 = "Lazyroar";
        System.out.println("Input: " + str1);
        System.out.println("Output: "
                           + isStringOnlyAlphabet(str1));
 
        // Checking for String with numeric characters
        System.out.println("\nTest Case 2:");
 
        String str2 = "Geeks4Geeks";
        System.out.println("Input: " + str2);
        System.out.println("Output: "
                           + isStringOnlyAlphabet(str2));
 
        // Checking for null String
        System.out.println("\nTest Case 3:");
 
        String str3 = null;
        System.out.println("Input: " + str3);
        System.out.println("Output: "
                           + isStringOnlyAlphabet(str3));
 
        // Checking for empty String
        System.out.println("\nTest Case 4:");
 
        String str4 = "";
        System.out.println("Input: " + str4);
        System.out.println("Output: "
                           + isStringOnlyAlphabet(str4));
    }
}


Output

Test Case 1:
Input: Lazyroar
Output: true

Test Case 2:
Input: Geeks4Geeks
Output: false

Test Case 3:
Input: null
Output: false

Test Case 4:
Input: 
Output: false

Complexity Analysis:

  • Time Complexity: O(N), where N is the length of the string.
  • Auxiliary Space: O(1), no extra space required so it is a constant.

Related Articles:

RELATED ARTICLES

Most Popular

Recent Comments