Z 323 Percentage of Vowels HackerRank Answer

 Bunny has a string. Bunny likes vowel and dislikes consonants so he would like to know what percentage of the characters in the string are vowels.

Input Format

Only one line of input that contains one string.

Constraints

One string that only contains lowercase or uppercase letters. The length of this string will not be greater than 100.

Output Format

Output the required percentage upto 4 decimal places.

Sample Input 0

bunnyyisastringlover

Sample Output 0

30.0000

Explanation 0

total characters : 20 vowels : 6 percentage = (6/20)*100 = 30

Source Code

#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

int main() {

    string a;

    cin>>a;

    int i,x=a.length();

    float z=1.0000,v=0.0;

    for(i=0;i<x;i++)

    {

        if(a[i]=='a' || a[i]=='e' || a[i]=='i' || a[i]=='o' || a[i]=='u' || a[i]=='A' || a[i]=='E' || a[i]=='I' || a[i]=='O' || a[i]=='U')

        {

            v=v+1;

        }

    }

    z=(v*100)/x;

    printf("%.4f",z);

    return 0;

}


Comments