Count Consonants In a String

Given a string ‘STR’ which consists of uppercase and lowercase characters and spaces. Count the number of consonants in the string.

A consonant is an English alphabet character that is not vowel (a, e, i, o, and u). Examples of constants are b, c, d, f, etc.

Example :

Given string 'STR' : ‘Coding Ninjas’ there are 8 consonants i.e ‘C’,’d’,’n’,’g’,’N’,’n’,’j’,’s’, because these characters do not belong to set above mentioned set of vowels.
Input Format :
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.

The only line of each test case consists of a string ‘STR’
Output Format :
For each test case, print a single integer denoting the number of consonants in the string.
Note :
You don't need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 50
1 <= Length of 'STR' <= 10^4

The string consists of uppercase and lowercase characters and spaces.

Time Limit: 1 sec
CodingNinjas
author
2y
Recursive Approach

If we can find if the current character is a consonant or not we can create a recursive to count all the vowels.

  1. Let ‘i’ be the index of the current character we are at and ‘N’ be th...read more
CodingNinjas
author
2y
Iterative Approach

We can simply check if every element is a consonant or not and keep the count of the number of consonants using a simple for loop.

Here is the algorithm :

  1. Create a variable (say, ‘CO...read more
Help your peers!
Add answer anonymously...
Optum Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter