Use string methods to find and count vowels in a string?

前端 未结 9 2102
青春惊慌失措
青春惊慌失措 2020-12-02 02:04

I have this problem for homework (I\'m being honest, not trying to hide it at least) And I\'m having problems figuring out how to do it.

Given the following declarat

9条回答
  •  借酒劲吻你
    2020-12-02 02:30

    I don't see the need to have a print statement in the loop.

    String s = "Whatever you want it to be.".toLowerCase();
    int vowelCount = 0;
    for (int i = 0, i < s.length(); ++i) {
        switch(s.charAt(i)) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                vowelCount++;
                break;
            default:
                // do nothing
        }
    }
    

    This converts the string to lowercase, and checks all the characters in the string for vowels.

提交回复
热议问题