Algorithm
Problem Name: 30 days of code -
Objective
Today, we're working with regular expressions. Check out the Tutorial tab for learning materials and an instructional video!
Task
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 , 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 lower case letters [a - z] only.
- Each of the email IDs consists of lower case 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
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int N,i,j;
scanf("%d",&N);
char *ptr,*str,*ptr1;
char tmp[20],tmp1[30];
char firstName[N][20];
char emailID[N][30],*emid;
for(i= 0; i < N; i++)
{
scanf("%s %s",firstName[i],emailID[i]);
}
for(i = 1; i < N; i++)
{
for(j = 1; j < N; j++)
{
if(strcmp(firstName[j-1],firstName[j])>0)
{
strcpy(tmp,firstName[j-1]);
strcpy(firstName[j-1],firstName[j]);
strcpy(firstName[j],tmp);
strcpy(tmp1,emailID[j-1]);
strcpy(emailID[j-1],emailID[j]);
strcpy(emailID[j],tmp1);
}
}
}
for(j = 0; j < N; j++)
{
//strcat(str,"gmail");
ptr=strstr(emailID[j],"@");
ptr1=strstr(emailID[j],"g");
if(strcmp(ptr,"@gmail.com")==0 && strncmp(ptr1,"gamil@gmail",8)!=0 )
printf("%s\n",firstName[j]>;
else
{}
}
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin >> N;
vector < string> emails(N);
vector<string> names(N);
vector < string> gmailUsers;
for(int i = 0; i < N; i++)
{
cin >> names[i] >> emails[i];
}
for(int i = 0; i < N; i++)
{
if(regex_match(emails[i], regex("(.*)@gmail.com")))
gmailUsers.push_back(names[i]);
}
sort(gmailUsers.begin(), gmailUsers.end());
for(int index = 0; index < gmailUsers.size(); index++>
{
cout << gmailUsers[index] << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
#3 Code Example with C# Programming
Code -
C# Programming
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Solution
{
static void Main(String[] args)
{
var n = int.Parse(Console.ReadLine());
var list = new List < string>();
for (int i = 0; i < n; i++)
{
var tmp = Console.ReadLine().Split(' ');
var name = tmp[0];
var email = tmp[1];
if (Regex.IsMatch(email, @".+@gmail\.com$"))
{
list.Add(name);
}
}
list.Sort();
foreach (var name in list)
{
Console.WriteLine(name);
}
}
}
Copy The Code &
Try With Live Editor
#4 Code Example with Golang Programming
Code -
Golang Programming
package main
import (
"fmt"
"regexp"
"sort"
)
func main() {
var n int
var name, mail string
var nameSlice []string
fmt.Scan(&n)
r, _ := regexp.Compile(`(@gmail\.com)$`)
for i := 0; i < n; i++ {
fmt.Scanln(&name, &mail)
if r.MatchString(mail) {
nameSlice = append(nameSlice, name)
}
}
sort.Slice(nameSlice, func(i, j int) bool {
return nameSlice[i] < nameSlice[j]
})
for _, v := range nameSlice {
fmt.Println(v)
}
}
Copy The Code &
Try With Live Editor
#5 Code Example with Java Programming
Code -
Java Programming
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
String emailRegEx = ".+@gmail\\.com$";
Pattern pattern = Pattern.compile(emailRegEx);
ArrayList < String> list = new ArrayList<>();
for (int i = 0; i < N; i++) {
String name = in.next();
String email = in.next();
Matcher matcher = pattern.matcher(email);
if (matcher.find()) list.add(name);
}
Collections.sort(list);
list.forEach(System.out::println);
}
}
Copy The Code &
Try With Live Editor
#6 Code Example with Javascript Programming
Code -
Javascript Programming
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function(data) {
input_stdin += data;
});
process.stdin.on('end', function() {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var N = parseInt(readLine()),
op = [];
for (var a0 = 0; a0 < N; a0++) {
var firstName_temp = readLine().split(' ');
var firstName = firstName_temp[0];
var emailID = firstName_temp[1];
if (/@gmail\.com/.test(emailID)) op.push(firstName);
}
console.log(op.sort().join("\n"));
}
Copy The Code &
Try With Live Editor
#7 Code Example with Python Programming
Code -
Python Programming
import re
arr = []
n = int(input())
for i in range(n):
data = str(input()).split(" ")
name = data[0]
email = data[1]
if re.search(".+@gmail\.com$", email):
arr.append(name)
arr.sort()
for name in arr:
print(name)
Copy The Code &
Try With Live Editor