C++ code to find out if a name is male or female

C++Server Side ProgrammingProgramming

C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

Suppose, we are given n strings in an array 'input'. The strings are names, we have to find out if they are male or female names. If a name ends with 'a', 'e', 'i', or 'y'; it can be said that it is a female name. we print 'male' or 'female' for each input in the string.

So, if the input is like n = 5, input = {"Lily", "Rajib", "Thomas", "Riley", "Chloe"}, then the output will be Female, Male, Male, Female, Female.

Steps

To solve this, we will follow these steps −

for initialize i := 0, when i < n, update (increase i by 1), do:
s := input[i]
l := size of s
if s[l - 1] is same as 'a' or s[l - 1] is same as 'e' or s[l - 1] is same as 'i' or s[l - 1] is same as 'y', then:
print("Female")
Otherwise,
print("Male")

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
#define N 100
void solve(int n, string input[]) {
for(int i = 0; i < n; i++) {
string s = input[i];
int l = s.size();
if (s[l - 1] == 'a' || s[l - 1] == 'e' || s[l - 1] == 'i' || s[l - 1] == 'y')
cout<< "Female" << endl;
else
cout << "Male" << endl;
}
}
int main() {
int n = 5;
string input[] = {"Lily", "Rajib", "Thomas", "Riley", "Chloe"};
solve(n, input);
return 0;
}

Input

5, {"Lily", "Rajib", "Thomas", "Riley", "Chloe"}

Output

Female
Male
Male
Female
Female
Updated on 29-Mar-2022 08:47:28