Check whether all the substrings have number of vowels atleast as that of consonants


In Alphabetical series there are consists of 26 characters of which 5 characters are vowels such as a, e, i, o, u and the rest are called as consonants. In C++, we have predefined functions such as tolower() and length() that will help to Check whether all the substrings have number of vowels atleast as that of consonants.

Let’s take an example of this.

  • The string has only one consonant with the rest vowel “aiteau”. Hence, it is accepted as the final result.

  • The string has more than one consonant with the rest vowel “appioa”. Hence, it is not accepted as the final result.

Using Simple for Loop

The program uses the two-function definition based on vowels and at least consonants. These functions provide some loops and operators that help to solve this problem statement.

Syntax

The following syntax is used in the examples

tolower()

The predefined function tolower() converts the any given character into lowercase.

length()

The length() is a predefined function of C++ that can be used to find the length of the given string.

Algorithm

The following steps are-

Step 1: Start the program by mentioning the header file iostream.

Step 2: Then use the function definition vowel_letter() that will be used to set the built-in function tolower() for conversion of characters into lowercase. Then return the function by specifying the vowel character with the help of operators- == and ||.

Step 3: Next, it will define another function definition named atleast_consonant() that accepts the parameter as str to fetch the input of a given string. This function operates to work on one consonant with the rest vowel. For example- aatuuie: The t is only consonant and the rest letters are vowels.

Step 4: Then using first for loop, the variable i iterates over the input string and connect to work the function definition vowel_letter by using some operator such as ! and && in the condition of if-statement. Return false if it is found as more than one consonant in the given string.

Step 5: Moving ahead to use the second for loop, where it will use the if statement to set the condition based && operator. If the vowel surrounding more than one consonant it returns false if all the condition satisfies otherwise true.

Step 6: Now start the main function of the program and initialize the input string.

Step 7: Using an if-else statement, it set the condition as a calling function to pass the input of the string and check whether there will be present atleast consonants or not.

Example

In the following example, we will use the two for loop in which the first loop iterates on the basis of one consecutive consonant character in the present given list whereas the second loop will use on the basis of vowel surrounded by more than one consonant to justify this problem.

#include <iostream>
using namespace std;
// function operate to work on vowel character
bool vowel_letter(char ch)
{   
// Using tolower() covert the character into a lowercase letter
    ch = tolower(ch); 
// Set the condition to check the character vowel by using the operator logical OR and equal to
    return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
// function operate to work on at least one consonant character present with the rest vowel.
bool atleast_consonant(const string& str)
{
    int n = str.length();
// Using the first iteration it checks if more than one consecutive consonant present
    for (int i = 1; i < n; i++)
    {
        if (!vowel_letter(str[i]) && !vowel_letter(str[i - 1]))
            return false;
    }
// Using the second iteration it checks if the vowel is surrounded by more than one consonant
    for (int i = 1; i < n - 1; i++)
    {
        if (vowel_letter(str[i]) && !vowel_letter(str[i - 1]) && !vowel_letter(str[i + 1]))
            return false;
    }
// return true when there exists only one consonant with vowels
    return true;
}
// Start the main function
int main()
{
    string stng = "abaue";
// Function call used under if-statement
    if (atleast_consonant(stng))
        cout << "The given problem statement is satisfied." << endl;
    else
        cout << "The given problem statement is not satisfied" << endl;
    
    return 0;
}

Output

The given problem statement is satisfied.

Conclusion

The vowel and consonants are the characters of the alphabet and that can be used in program activity to solve the specific problem. There were used some predefined functions such as tolower() and length() fulfilled the specific need when required. This type of generally used to solve the algorithmic problem statement.

Updated on: 16-Aug-2023

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements