ronith
8/16/2018 - 11:16 AM

Calculate the difficulty of a sentence

Calculate difficulty of a given sentence. Here a Word is considered hard if it has 4 consecutive consonants or number of consonants are more than number of vowels. Else word is easy. Difficulty of sentence is defined as 5(number of hard words) + 3(number of easy words).

Examples:

Input : str = "Difficulty of sentence" Output : 13 Hard words = 2(Difficulty and sentence) Easy words = 1(of) So, answer is 52+31 = 13

Algo: Start traversing the string and perform following steps:-

  1. Increment vowels count, if current character is vowel and set conecutive consonants count=0.
  2. Else increment consonants count, also increment consecutive consonants count.
  3. Check if consecutive consonants becomes 4, then current word is hard, so increment its count and move to the next word.Reset all counts to 0.
  4. Else check if a word is completed and count of consonants is greater than count of vowels, then it is a hard word else easy word.Reset all counts to 0.
// https://www.geeksforgeeks.org/calculate-difficulty-sentence/
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

bool isvowel (char c) {
    if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

int difficulty (string s) {
    int cc=0, vowel=0, conso=0, h=0, e=0;
    for (int i=0; i<s.length();i++) {
        if (s[i] != ' ' && isvowel(tolower(s[i]))) {
            vowel++;
            cc=0;
        }
        else if (s[i]!= ' '){
            cc++;
            conso++;
        }
        if (cc==4) {
            h++;
            while (i<s.length() && s[i] != ' ')
                i++;
            cc=0;
            vowel=0;
            conso=0;
        }
        else if (i< s.length() && (s[i]== ' ' || i==s.length()-1)) {
            conso>vowel? h++:e++;
            cc=0;
            vowel=0;
            conso=0;
        }
    }
    return 5*h+3*e;
}

int main () {
    string s;
    getline(cin ,s);
    cout<< difficulty (s);

}