Home > Blockchain >  How to tell if the last letter of the word in a string is a consonant, vowel, number, or a special c
How to tell if the last letter of the word in a string is a consonant, vowel, number, or a special c

Time:11-15

i tried to search in the google but i only saw how to count the overall string but what i need to know if the last letter of the word i put in my Command Line Argument code is a consonant, vowel, a number, or a special character.

For example: (Output) Stackoverflow = Consonant the code used the letter "w" which is the last letter of the word to know that it's a consonant. Close$ = Special Character here the code used the symbol "$" to determine that the last letter is a special symbol.

I tried to use the if-else method but I don't know how to continue with it.

CodePudding user response:

Another lazy approach could be:

    String specialChars = "!#$%&'()* ,-./:;<=>?@[]^_`{|}";
    String numbers = "0123456789";
    String vowels = "aeiou";
    String consonants = "bcdfghjklmnpqrstvwxyz";
    
    for (int i=0 ; i<args.length; i  ) {
        String c = args[i].substring(args[i].length() - 1);
        if (specialChars.contains(c))
            System.out.println(args[i]   " = Special Char");
        else if (numbers.contains(c))
            System.out.println(args[i]   " = Number");
        else if (consonants.contains(c))
            System.out.println(args[i]   " = Consonants");
        else if (vowels.contains(c))
            System.out.println(args[i]   " = Vowel");
        else
            System.out.println(args[i]   " = Unknown");
    }

CodePudding user response:

I didn't get if you need to recognize the character '$' if you do then you should take a look to ASCII table anyway i hope that could help:

public void getChar(String str){
    char ch = str.toUpperCase().charAt(str.length()-1); //take last char in uppercase
    
    if(ch < 65 || ch > 90) //lesser than A or higer than Z
        System.out.println("special char");
    
    else    
       if(ch == 65 || ch == 69 || ch == 73 || ch == 79 || ch == 85) //vowels dec values
            System.out.println("vowel");
        
    else System.out.println("consonant");
        
}
  • Related