We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<regex.h>#define MAXNAME 21#define MAXEMAIL 51intstrcmp_p(constvoid*a,constvoid*b){constchar**a_pp=(constchar**)a;constchar**b_pp=(constchar**)b;returnstrcmp(*a_pp,*b_pp);}intmain(){intnInputs;scanf("%d",&nInputs);char**gmail_accounts=(char**)malloc(nInputs*sizeof(char*));/*copy all names with gmail accounts unto an array of strings*/regex_tregex;regcomp(®ex,"[a-zA-Z]*@gmail.com",0);intnNames=0;for(inti=0;i<nInputs;i++){charname[MAXNAME],emailID[MAXEMAIL];scanf("%s %s",name,emailID);if(regexec(®ex,emailID,0,NULL,0)){continue;}gmail_accounts[nNames]=malloc(strlen(name)*sizeof(char));strcpy(gmail_accounts[nNames++],name);}/*sort array of names and print*/qsort(gmail_accounts,nNames,sizeof(gmail_accounts[0]),strcmp_p);for(inti=0;i<nNames;i++){printf("%s\n",gmail_accounts[i]);free(gmail_accounts[i]);}free(gmail_accounts);returnEXIT_SUCCESS;}
I'm new to coding, and VERY new to C coding. How come you used memory allocation for the strings you scanned? Also, how come you only dealloc the double pointer gmailnames, but not every string in it?
I know you did this a long time, so I'm just posting this mainly for posterity :p
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 28: RegEx, Patterns, and Intro to Databases
You are viewing a single comment's thread. Return to all comments →
Interesting. Your code is similar to mine own.
I'm new to coding, and VERY new to C coding. How come you used memory allocation for the strings you scanned? Also, how come you only dealloc the double pointer gmailnames, but not every string in it? I know you did this a long time, so I'm just posting this mainly for posterity :p