Count vowels, consonants, and spaces

Easy
0/40
Average time to solve is 10m
profile
Contributed by
9 upvotes
Asked in companies
SamsungGoldman SachsCapegemini Consulting India Private Limited

Problem statement

Given a string, write a program to count the number of vowels, consonants, and spaces in that string.

EXAMPLE :
Input: ‘N’= 25, ‘s’ =”Take u forward is Awesome”
Output: 10 11 4
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line will contain the integer 'T', the number of test cases.

Each test case consists of two lines.

The first line of input contains one integer, 'N' the length of the given string.

Followed by a line containing the given string. All the characters in the input are lowercase or uppercase Latin letters or spaces.
Output format :
For each test case, print three space-delimited integers denoting the number of vowels, consonants, and spaces in the given string respectively.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= 'T' <= 10
1 <= 'N' <= 10^5
It is guaranteed that sum of ‘N’ over all test cases is <= 10^5
Time Limit: 1 sec
Sample Input 1 :
2
25
Take u forward is Awesome
27
India won the cricket match
Sample Output 1 :
10 11 4
8 15 4
Explanation Of Sample Input 1 :
Note: Ignore the inverted commas at the start and end of the string, they are just to make it visually better to understand the given string.
The first test case is the same as the explanation above.

For the second test case, the vowels are at indices {1, 4, 5, 8, 13, 17, 20, 24} and spaces are at indices {6, 10, 14, 22} rest indices are consonants i.e. (27 - 8 - 4) = 15.

Hence, the output will be: 8 15 4
Sample Input 2 :
2
12
hEllOWorlD
2
Sample Output 2 :
3 7 2
0 0 2
Hint

Take care of cases when no letters are there in string or capital vowel letters are present.

Approaches (1)
Optimal Approach

The idea is to iterate through the given string, if we encounter a vowel we increment the count of vowels, else if we encounter a space we increment the count of spaces else we increment the count of consonants.

The vowels are {a, e, i, o, u, A, E, I, O, U}.

So to make the implementation easier we will convert our whole string into lowercase and then we just have to check if the current character is one of {a, e, i, o, u} or not.
 

Algorithm:
 

// The function will convert the provided string into lowercase.

void toLowercase(s[], n)

  • Run a loop from 0 to ‘n-1’ with a variable ‘i’
    • If ‘s[i]>=A and s[i]<=Z’
      • Assign ‘s[i]’ value of ‘a - A + s[i]’

Bool isVowel(ch)

  • If ‘ch==a or ch==e or ch==i or ch==o or ch==u’ return ‘true’
  • Else return ‘false’

Int[] countVowelsConsonantsSpaces(s[], n)

  • Initialize array ‘ans’ of size three with initial values as zero.
  • Call ‘toLowercase(s, n)’.
  • Run a loop from 0 to ‘n-1’ with a variable ‘i’
    • If ‘isVowel(s[i])’ Increment count of ‘ans[0]’ by one.
    • Else if ‘s[i] == space’ Increment count of ‘ans[2]’ by one.
    • Else Increment count of ‘ans[1]’ by one.
  • Return ‘ans’
Time Complexity

O(N), Where ‘N’ is the input integer.
 

Here we are iterating on the given string twice and for each character of the string we check if it is a vowel, space, or consonant, hence the time complexity will be O(N)

Space Complexity

O(1)


Here we are using only an array of size 3 to store the count of the number of vowels, the number of spaces, and the number of consonants, hence the space complexity will be O(1), i.e. constant.

Code Solution
(100% EXP penalty)
Count vowels, consonants, and spaces
Full screen
Console