# Day 28 RegEx, Patterns, and Intro to Databases Solution

Day 28 RegEx, Patterns, and Intro to Databases Hackerrank Solution in C++. Today, we're working with regular expressions. Consider a database table, Emails, which has the attributes First Name and Email ID. Given N rows of data simulating the Emails table, print an alphabetically-ordered list of people whose email address ends in @gmail.com.

Input Format

The first line contains an integer, N, the total number of rows in the table.
Each of the N subsequent lines contains 2 space-separated strings denoting a person's first name and email ID, respectively.

Constraints

• 2 <= N <= 30
• Each of the first names consists of lowercase letters [a - z] only.
• Each of the email IDs consists of lowercase letters [a - z], @ and . only.
• The length of the first name is no longer than 20.
• The length of the email ID is no longer than 50.

Output Format

Print an alphabetically-ordered list of first names for every user with a Gmail account. Each name must be printed on a new line.

Sample Input

• 6
• riya riya@gmail.com
• julia julia@julia.me
• julia sjulia@gmail.com
• julia julia@gmail.com
• samantha samantha@gmail.com
• tanya tanya@gmail.com

Sample Output

• julia
• julia
• riya
• samantha
• tanya

Print an alphabetically-ordered list of first names for every user with a Gmail account. Each name must be printed on a new line.

## Day 28 RegEx, Patterns, and Intro to Databases Hackerrank Solution in C++

``````#include <bits/stdc++.h>
using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

bool compare(string a, string b)
{
return a < b;
}

int main()
{
string N_temp;
getline(cin, N_temp);

int N = stoi(ltrim(rtrim(N_temp)));

for (int N_itr = 0; N_itr < N; N_itr++)
{
string first_multiple_input_temp;
getline(cin, first_multiple_input_temp);

vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));

string firstName = first_multiple_input;

string emailID = first_multiple_input;

if (regex_match(firstName, regex("[a-z]{1,20}")) and regex_match(emailID, regex("[a-z.]+@gmail.com{1,50}")))
}

for (auto i: answers) cout << i << endl;

return 0;
}

string ltrim(const string &str)
{
string s(str);

s.erase(		s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int> (isspace)))
);

return s;
}

string rtrim(const string &str)
{
string s(str);

s.erase(		find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int> (isspace))).base(),
s.end()
);

return s;
}

vector<string> split(const string &str)
{
vector<string> tokens;

string::size_type start = 0;
string::size_type end = 0;

while ((end = str.find(" ", start)) != string::npos)
{
tokens.push_back(str.substr(start, end - start));

start = end + 1;
}

tokens.push_back(str.substr(start));

}``````

### RegEx, Patterns, and Intro to Databases Explanation

RegEx, Patterns, and Intro to Databases Solution are the same as SQL Query. First, we have a database table consist first name and email Now we have to print the name of the person whose email ID ends with @gmail.com. So the first step is to take a regular expression like this "regex exp(".+@gmail\\.com\$");" and now with the help of a string array, we will take an input Fname and Eid and compare Eid with a regular expression.

If Eid is matched with a regular expression then we store the name of the person in a String array. Now the last step is to print the string array in order. For a better explanation let's take an example of the above Sample Input, here number 6 ( total number of input ) and a first name and Email ID. As you can see the first email is riya@gmail.com this is correct so store or push into a string array.

Now come to the second Email ID julia@julia.me as we can see there is no matching with @gmail.com then ignore this name and Email ID same for the next and next to next and continue all have passed the condition. Now next step is to print the First name in ascending order.

Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.