Arrays & Hashing

String Challenge-3

Estimated reading: 1 minute 33 views

Vowel Count Challenge

Create a function countVowels(input) that accepts a string input and returns the total number of vowels (a, e, i, o, u) found in the string. For example, in the sentence “The quick brown fox” the result would be 5. Note that the letter ‘y’ should not be considered a vowel for this task.

Examples:

  • Input: "world" Output: 1

  • Input: "programming" Output: 3

				
					 public static int countVowels(String input) {
        // Convert the input string to lowercase to make the comparison case-insensitive
        input = input.toLowerCase();
        
        // Initialize a counter to keep track of the number of vowels
        int vowelCount = 0;
        
        // Loop through each character in the string
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            
            // Check if the current character is a vowel
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                vowelCount++;
            }
        }
        
        // Return the total count of vowels
        return vowelCount;
    }
				
			
Share this Doc

String Challenge-3

Or copy link

CONTENTS