Accept a string as parameter. Find out how many consonants present in it.





Test cases:

Test cases Expected Result
countConsonents(instanceof is for references only) –>[18] 18 OK
countConsonents(Object Oriented) –>[8] 8 OK
countConsonents(Design Patterns88) –>[10] 10 OK

Java solution:

public class Program7 {
	public static void main(String[] args) {
		Program7.countConsonents("instanceof is for references only");
		Program7.countConsonents("Object Oriented");
		Program7.countConsonents("Design Patterns88");
	}
	public static int countConsonents(String s) {
		int count = 0;
		String str = "bcdfghjklmnpqrstvwxyz";
		String array1[] = s.split("");
		for (int i = 0; i < s.length(); i++) {
			if (str.contains(array1[i].toLowerCase())) {
				count++;
			}
		}
		return count;
	}
}

Javascript solution:

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

console.log(countConsonents("instanceof is for references only")); //18
console.log(countConsonents("Object Oriented")); //8
console.log(countConsonents("Design Patterns")); //10

 

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