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

前端 未结 9 2434

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:48

    following code will give you vowel and Constonent count

    static String VOWEL_GROUP = "AEIOUaeiou";
    static String testString = "AAAASHMAIOUAXCCDIOUGGGGA"; // say this is your text
    
    public static void main(String[] args) {
        int vovelCount = 0;
         int consonantCount = 0;
        for (int j = testString.length() - 1; j >= 0; j--) {//outer loop 
            for (int i = 0; i < VOWEL_GROUP.length(); i++) { //inner loop
                if (VOWEL_GROUP.charAt(i) == testString.charAt(j)) {
                    vovelCount++; //vowel count in text
                    break;
                }else{
                    consonantCount ++;
                }
            }
        }
        System.out.println(vovelCount+" "+ consonantCount);
    }
    

提交回复
热议问题