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

前端 未结 9 2440

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-06 23:43

    Here is the simple code for counting the number of vowels using recursion

     public static int vowels(String s){
            int count =0;
            char c;
            if(s.length()==0){
                return 0;
            }
            else{
                c =s.charAt(0);
                if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
                    count++;
                }
                return count+vowels(s.substring(1));
    
    
            }
        }
    

提交回复
热议问题