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.
voidfree_ptr_ptr(char**arr,unsignedintlen){for(inti=0;i<len;i++){free(*(arr+i));}free(arr);}char**split(char*s,constchardelim,int*count){char**split_s=NULL;char*part=NULL;unsignedintidx_split_s=0;unsignedintidx_part=0;charc='\0';for(unsignedinti=0;*(s+i)!='\0';i++){c=*(s+i);if(c==delim&&idx_part!=0){// mark the end of part stringpart=realloc(part,sizeof(char)*(idx_part+1));*(part+idx_part)='\0';// append part to split_ssplit_s=realloc(split_s,sizeof(char*)*(idx_split_s+1));*(split_s+idx_split_s)=part;idx_split_s++;// clear part string valuespart=NULL;idx_part=0;}else{// append character to partpart=realloc(part,sizeof(char)*(idx_part+1));*(part+idx_part)=c;idx_part++;}}// append last part to split_sif(idx_part!=0){// mark the end of part stringpart=realloc(part,sizeof(char)*(idx_part+1));*(part+idx_part)='\0';// append part to split_ssplit_s=realloc(split_s,sizeof(char*)*(idx_split_s+1));*(split_s+idx_split_s)=part;idx_split_s++;}*count=idx_split_s;returnsplit_s;}structdocumentget_document(char*text){int*paragraph_count=calloc(1,sizeof(int));int*sentence_count=calloc(1,sizeof(int));int*word_count=calloc(1,sizeof(int));// split into paragraphs and store temporarilychar**paragraphs_temp=split(text,'\n',paragraph_count);structdocumentdocument;document.paragraph_count=*paragraph_count;// allocate space for paragraphs in documentdocument.data=malloc(*paragraph_count*sizeof(structparagraph));char*s=NULL;for(inti=0;i<*paragraph_count;i++){s=*(paragraphs_temp+i);// get current paragraph// split into sentences and store temporarilychar**sentences_temp=split(s,'.',sentence_count);structparagraphparagraph;paragraph.sentence_count=*sentence_count;// allocate space for sentences in each paragraphparagraph.data=malloc(*sentence_count*sizeof(structsentence));for(intj=0;j<*sentence_count;j++){s=*(sentences_temp+j);// get current sentence// split into words and store temporarilychar**words_temp=split(s,' ',word_count);// *(*(document + i) + j) = malloc(*word_count * sizeof(char*));structsentencesentence;sentence.word_count=*word_count;// allocate space for words in each sentencesentence.data=malloc(*word_count*sizeof(structword));for(intk=0;k<*word_count;k++){s=*(words_temp+k);// get current wordstructwordword;word.data=s;*(sentence.data+k)=word;}// end of word_count loop*(paragraph.data+j)=sentence;free(words_temp);}// end of sentence_count loop*(document.data+i)=paragraph;free_ptr_ptr(sentences_temp,*sentence_count);}// end of paragraph_count loopfree_ptr_ptr(paragraphs_temp,*paragraph_count);free(word_count);free(sentence_count);free(paragraph_count);returndocument;}structwordkth_word_in_mth_sentence_of_nth_paragraph(structdocumentDoc,intk,intm,intn){// return *((*((*(Doc.data + --n)).data + --m)).data + --k);structparagraphnth=*(Doc.data+--n);structsentencemth=*(nth.data+--m);structwordkth=*(mth.data+--k);returnkth;}structsentencekth_sentence_in_mth_paragraph(structdocumentDoc,intk,intm){// return *((*(Doc.data + --m)).data + --k);structparagraphmth=*(Doc.data+--m);structsentencekth=*(mth.data+--k);returnkth;}structparagraphkth_paragraph(structdocumentDoc,intk){return*(Doc.data+--k);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Structuring the Document
You are viewing a single comment's thread. Return to all comments →