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.
char*kth_word_in_mth_sentence_of_nth_paragraph(char****document,intk,intm,intn){returndocument[n-1][m-1][k-1];}char**kth_sentence_in_mth_paragraph(char****document,intk,intm){returndocument[m-1][k-1];}char***kth_paragraph(char****document,intk){returndocument[k-1];}voidread_word(char**pool,char***words,char*beg,char*end){intlen=end-beg;memcpy(*pool,beg,len);*(*words)++=*pool;*(*pool+len+1)='\0';*pool+=len+1;}char****get_document(char*text){// Make a first pass to calculate the amount of memory we need.intnum_paras=0;intnum_sens=0;intnum_words=0;inttext_size=0;for(char*p=text,c=*p;c;c=*++p){switch(c){case'\n':++num_paras;break;case'.':++num_sens;// fallthroughcase' ':++num_words;// fallthroughdefault:++text_size;// +1 for . and ' ' accounts for the space needeed for NULs}}// Build the document structurechar****paras=malloc(num_paras*sizeof(char***)),****doc=paras;char***sens=malloc(num_sens*sizeof(char**));char**words=malloc(num_words*sizeof(char*));char*pool=malloc(text_size);char*word=NULL;*sens=words;*paras=sens;for(char*p=text,c=*p;c;c=*++p){if(c=='\n')*++paras=sens;elseif(c==' '||c=='.'){read_word(&pool,&words,word,p);if(c=='.'&&*(p+1))*++sens=words;word=NULL;}elseif(word==NULL)word=p;}returndoc;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Querying the Document
You are viewing a single comment's thread. Return to all comments →