How to count vowels and consonants and capitalizing first letter in a string while output using a method and return result

前端 未结 9 2416

If a user enters a string: hello there

it should output

Hello has 2 vowels
There has 3 consonants.

I know this is a f

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 00:05

    Here is a simpler way of doing this, hope this helps:

    public static void checkVowels(String s){
        System.out.println("Vowel Count: " + (s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length()));
        //Also eliminating spaces, if any for the consonant count
        System.out.println("Consonant Count: " + (s.toLowerCase().replaceAll("a|e|i|o| |u", "").length()));
    }
    

提交回复
热议问题