#include using namespace std; bool is_balanced(const string& str) { static const string vowels = "aeiou"; size_t len = str.size(); size_t half = len/2; size_t start_second = (len % 2) ? half + 1 : half; int count1 = 0, count2 = 0; for (size_t i = 0; i < half; ++i) { char c = tolower(static_cast(str[i])); if (vowels.find(c) != string::npos) ++count1; } for (size_t i = start_second; i < len; ++i) { char c = tolower(static_cast(str[i])); if (vowels.find(c) != string::npos) ++count2; } return count1 == count2; } int main() { const string case_values[] = { "racecar", "Lorem Ipsum", "Kitty Ipsum", "string", " ", "abcdefghijklmnopqrstuvwxyz", "123A#b!E&*456-o.U" }; for (const string s : case_values) { cout << boolalpha << is_balanced(s) << endl; cout << "-----------------------------------" << endl; } } //Notes: //std::boolalpha is a manipulator in C++ that allows you to output //boolean values as "true" or "false" //string::npos is a constant (probably -1) representing a non-position. // It's returned by method find when the pattern was not found.