C++ code to find answers by vowel checking



In this article, we will explain a beginner level problem that involves finding answers for questions by checking for vowels in the question text. We will implement a C++ program that solves this problem. Let's break down the problem statement below.

Find Answers by Vowel Checking

Amal and Bimal are playing a game. Amal will ask any questions whose answers will be either "Yes" or "No". If the question's last letter is a vowel, then Bimal will answer "Yes" otherwise "No". You are given a string containing the question that Amal asks. Your task is to determine the answer based on the last letter of the question and return "Yes" or "No".

Scenario 1

Input: "Is it raining?"

Output: "No"

Explanation: Ignoring the question mark, the last letter is 'g', which is not a vowel. Therefore, the answer is "No".

Scenario 2

Input: "Is the color of your shirt blue?"

Output: "Yes"

Explanation: Again, ignoring the question mark, the last letter is 'e', which is a vowel. Therefore, the answer is "Yes".

Algorithm to Find Answers by Vowel Checking

To solve this problem, we can follow these steps:

  • Input the question as a string str of size n.
  • Define a set of vowels: {'a', 'e', 'i', 'o', 'u'}.
  • Check the last character of the string ( i.e., str[n-1]), if it is not a question mark, check if str[n-1] one of an element in the set of vowels (a, e, i, o, u).
  • If it is a vowel, return "Yes". Otherwise, return "No".
  • If the last character is a question mark, check the second last character (i.e., str[n-2]), and do the same check for vowels.

C++ Program to Find Answers by Vowel Checking

Here is the C++ code that implements the above algorithm, You can change the input string str to test with different questions.

#include <iostream>
#include <string>
#include <set>
using namespace std;

int main() {
    string str = "Is it raining?"; // Input question
    int n = str.length();
    set<char> vowels = {'a', 'e', 'i', 'o', 'u'};

    
    
    if (str[n - 1] == '?') {
        if (n > 1 && vowels.count(str[n - 2])) {
            cout << "Yes" << endl; // Last character is a vowel
        } else {
            cout << "No" << endl; // Last character is not a vowel
        }
    } 
    
    // If the last character is not a question mark
    else {
        if (vowels.count(str[n - 1])) {
            cout << "Yes" << endl; // Last character is a vowel
        } else {
            cout << "No" << endl; // Last character is not a vowel
        }
    }

    return 0;
}

The output of the above code will be:

No

Conclusion

In this article, we discussed a simple problem of determining answers to questions based on the last letter of the question. We implemented a C++ program that checks if the last letter is a vowel or not and returns "Yes" or "No" accordingly. This problem can be extended to more complex scenarios, but the basic logic is same.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-08-04T19:02:44+05:30

232 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements