Pass a string as parameter. Find out how many vowels present in it





Test cases:

Test cases Expected Result
countVowels(instanceif is for references only) 11 OK
countVowels(Object Oriented) 6 OK
countVowels(Design Patterns) 4 OK

Java solution:

public class Program6 {
	public static void main(String[] args) {
		Program6.countVowels("instanceif is for references only");
		Program6.countVowels("Object Oriented");
		Program6.countVowels("Design Patterns");
	}
	public static int countVowels(String s){
		int count = 0;
		String str = "aeiou";
		String array1[] = s.split("");
		for (int i = 0; i < s.length(); i++) {
			if (str.contains(array1[i].toLowerCase())) {
				count++;
			}
		}
		return count;
	}
}

Javascript solution:

const countVowels = (str) => {
    let count = 0;
    const vowels = 'aeiou';
    for (let i = 0; i < str.length; i++) {
        if (vowels.includes(str[i].toLowerCase())) {
            count++;
        }
    }
    return count;
}

console.log(countVowels("instanceof is for references only")); //11
console.log(countVowels("Object Oriented")); //6
console.log(countVowels("Design Patterns")); //4

 

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply