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:-
// 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);
}