Find Character Case in Java
In Java, character case checking involves determining if a given character is an uppercase alphabet, a lowercase alphabet, or not an alphabet at all.
1 if the character is an uppercase alphabet (A-Z)
0 if the character is a lowercase alphabet (a-z)
-1 if the character is not an alphabet
Example:
Input: char inputChar = 'G';
Output: 1
Explanation: Since 'G' is an uppercase alphabet, the output is 1.
Approach: Using Character Class Methods
Algorithm
Step 1: Use the method to determine if the character is uppercase.
Step 2: If the method returns true, return 1.
Step 3: Use the method to determine if the character is lowercase.
Step 4: If the method returns true, return 0.
Step 5: If the above two checks fail, return -1.
FileName: CharacterCheck.java
public class CharacterCheck { // Method to check if the character is uppercase, lowercase, or not an alphabet public static int checkCharacter(char inputChar) { if (Character.isUpperCase(inputChar)) { return 1; // Return 1 if the character is uppercase } else if (Character.isLowerCase(inputChar)) { return 0; // Return 0 if the character is lowercase } else { return -1; // Return -1 if the character is not an alphabet } } public static void main(String[] args) { char inputChar = 'G'; System.out.println(checkCharacter(inputChar)); } }
Output:
1
Complexity Analysis:
Time Complexity: O(1), as each method call performs a constant time check.
Space Complexity: O(1), since no additional space is used beyond a few variables.
Approach: Using ASCII Values
Algorithm
Step 1: Determine if the character's ASCII value is between 65 and 90.
Step 2: If the condition is true, return 1.
Step 3: Determine if the character's ASCII value is between 97 and 122.
Step 4: If the condition is true, return 0.
Step 5: If the above two conditions fail, return -1.
FileName: CharacterCheck1.java
public class CharacterCheck1 { // Method to check if the character is uppercase, lowercase, or not an alphabet using ASCII values public static int checkCharacter(char inputChar) { if (inputChar >= 65 && inputChar <= 90) { return 1; // Return 1 if the character is uppercase (A-Z) } else if (inputChar >= 97 && inputChar <= 122) { return 0; // Return 0 if the character is lowercase (a-z) } else { return -1; // Return -1 if the character is not an alphabet } } public static void main(String[] args) { char inputChar = 'G'; System.out.println(checkCharacter(inputChar)); } }
Output:
1
Complexity Analysis:
Time Complexity: O(1), as it involves a constant number of comparisons.
Space Complexity: O(1), since it uses only a fixed amount of space for variables.
Approach: Using Regular Expressions
Algorithm
Step 1: Convert the character to a string and check if it matches the pattern [A-Z].
Step 2: If the pattern matches, return 1.
Step 3: Convert the character to a string and check if it matches the pattern [a-z].
Step 4: If the pattern matches, return 0.
Step 5: If the above two patterns do not match, return -1.
FileName: CharacterCheck2.java
import java.util.regex.*; public class CharacterCheck2 { // Method to check if the character is uppercase, lowercase, or not an alphabet using regular expressions public static int checkCharacter(char inputChar) { String str = Character.toString(inputChar); if (str.matches("[A-Z]")) { return 1; // Return 1 if the character is uppercase } else if (str.matches("[a-z]")) { return 0; // Return 0 if the character is lowercase } else { return -1; // Return -1 if the character is not an alphabet } } public static void main(String[] args) { char inputChar = 'G'; System.out.println(checkCharacter(inputChar)); } }
Output:
1
Complexity Analysis:
Time Complexity: O(1), because matching a single character against a regular expression is a constant time operation.
Space Complexity: O(1), since only a fixed amount of space is required for variables and regex patterns.
Approach: Using Switch-Case
Algorithm
Step 1: Use a switch-case structure with cases for all uppercase letters (A to Z).
Step 2: If a case matches, return 1.
Step 3: Use a switch-case structure with cases for all lowercase letters (a to z).
Step 4: If a case matches, return 0.
Step 5: If no case matches (default case), return -1.
FileName: CharacterCheck3.java
public class CharacterCheck3 { // Method to check if the character is uppercase, lowercase, or not an alphabet using switch-case public static int checkCharacter(char inputChar) { switch (inputChar) { case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': return 1; // Return 1 if the character is uppercase case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': return 0; // Return 0 if the character is lowercase default: return -1; // Return -1 if the character is not an alphabet } } public static void main(String[] args) { char inputChar = 'G'; System.out.println(checkCharacter(inputChar)); } }
Output:
1
Complexity Analysis:
Time Complexity: O(1), as the switch-case involves a fixed number of constant time checks.
Space Complexity: O(1), because it requires only a fixed amount of space for variables.