Write Salesforce Program to Check Whether an Alphabet is Vowel or Consonant.
- Abhilash Banpurkar
- Jan 20, 2023
- 1 min read
Here is an example program in Salesforce Apex code that checks whether a given alphabet is a vowel or consonant:
public class AlphabetChecker {
public static String checkVowelConsonant(String alphabet) {
if (alphabet.length() > 1) {
return 'Please enter a single alphabet.';
}
else {
alphabet = alphabet.toUpperCase();
if (alphabet == 'A' || alphabet == 'E' || alphabet == 'I' || alphabet == 'O' || alphabet == 'U') {
return 'Vowel';
}
else {
return 'Consonant';
}
}
}
}
You can use the above class by instantiating an object of the class and calling the method checkVowelConsonant() and passing a single alphabet as the argument.
AlphabetChecker ac = new AlphabetChecker();
System.debug(ac.checkVowelConsonant('a'));
This will return "Vowel"
AlphabetChecker ac = new AlphabetChecker();
System.debug(ac.checkVowelConsonant('b'));
This will return "Consonant"
Please note that the above code assumes that the input is a single alphabet in uppercase or lowercase, if it's not it will throw an error.
Comments