Day 28: RegEx, Patterns, & Intro to Databases- 30 Days Code - IndianTechnoEra
Latest update Android YouTube

Day 28: RegEx, Patterns, & Intro to Databases- 30 Days Code

Introduction:

Welcome to Day 28 of the 30 Days of Code challenge! Today's task involves working with regular expressions. The objective is to consider a database table, Emails, with attributes First Name and Email ID. Given rows of data simulating the Emails table, we need to print an alphabetically-ordered list of people whose email address ends in '@gmail.com'.


The Challenge:

Write a program that reads data from the Emails table, extracts first names of users with Gmail accounts, and prints them in alphabetical order.


Constraints:

Each first name consists of lowercase letters only.

Each email ID consists of lowercase letters, numbers, and symbols (@, .) only.

The length of the first name is no longer than 20.

The length of the email ID is no longer than 50.


Format & Sample:

Input Format:

The first line contains an integer, N, the total number of rows in the table.

Each of the subsequent N lines contains two space-separated strings denoting a person's first name and email ID.


Input Format:

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


Output Format:

julia

julia

riya

samantha

tanya


Explanation:

The output consists of first names of users with Gmail accounts, ordered alphabetically.


Code Breakdown:

Read the number of rows, N, from input.

Loop over each row and extract the first name and email ID.

Use a regular expression to check if the email ID ends with '@gmail.com'.

If it does, add the first name to a list.

Sort and print the list.


Reading Input:

int main() {

    int N;

    cin >> N; // Read the number of rows

    vector<string> gmailUsers; // To store first names of Gmail users


    for (int i = 0; i < N; i++) {

        string firstName, emailID;

        cin >> firstName >> emailID; // Read first name and email ID

        // Check if the email ID ends with '@gmail.com'

        if (regex_match(emailID, regex(".+@gmail\\.com"))) {

            gmailUsers.push_back(firstName); // Add first name to the list

        }

    }

Printing Output:

    // Sort the list of first names alphabetically

    sort(gmailUsers.begin(), gmailUsers.end());


    // Print each name on a new line

    for (const string &name : gmailUsers) {

        cout << name << endl;

    }


    return 0;

}


Final Code:

#include <iostream>

#include <vector>

#include <regex>

#include <algorithm>


using namespace std;


int main() {

    int N;

    cin >> N; // Read the number of rows

    vector<string> gmailUsers; // To store first names of Gmail users


    for (int i = 0; i < N; i++) {

        string firstName, emailID;

        cin >> firstName >> emailID; // Read first name and email ID

        // Check if the email ID ends with '@gmail.com'

        if (regex_match(emailID, regex(".+@gmail\\.com"))) {

            gmailUsers.push_back(firstName); // Add first name to the list

        }

    }


    // Sort the list of first names alphabetically

    sort(gmailUsers.begin(), gmailUsers.end());


    // Print each name on a new line

    for (const string &name : gmailUsers) {

        cout << name << endl;

    }


    return 0;

}

This completes Day 28 of the 30 Days of Code challenge. You've successfully tackled a problem involving regular expressions and sorting. Great job!

إرسال تعليق

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.