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.
Solution which uses a NULL pointer as a delimiter for the array of words/sentences/paragraphs so that the length of the array can be determined dynamically...
typedefchar*WordType;typedefchar**SentenceType;typedefchar***ParagraphType;typedefchar****DocumentType;typedefstruct{WordTypecurr_word;SentenceTypecurr_sentence;ParagraphTypecurr_para;DocumentTypecurr_doc;}PointersType;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];}intwords_in_sentence(constSentenceTypesentence){inti=0;while(sentence&&sentence[i]!=NULL)i++;returni;}intsentences_in_paragraph(constParagraphTypeparagraph){inti=0;while(paragraph&¶graph[i]!=NULL)i++;returni;}intparagraphs_in_document(constDocumentTypedocument){inti=0;while(document&&document[i]!=NULL)i++;returni;}voidend_word(PointersType*p){// Add the current word to the sentence, making space for it - if there is a wordif(p->curr_word){intnum=words_in_sentence(p->curr_sentence);p->curr_sentence=realloc(p->curr_sentence,(2+num)*sizeof(WordType));p->curr_sentence[num]=p->curr_word;p->curr_sentence[num+1]=NULL;// Clear down current wordp->curr_word=NULL;}}voidend_sentence(PointersType*p){// Add the current sentence to the paragraph, making space for it - if there is a sentenceif(p->curr_sentence){intnum=sentences_in_paragraph(p->curr_para);p->curr_para=realloc(p->curr_para,(2+num)*sizeof(SentenceType));p->curr_para[num]=p->curr_sentence;p->curr_para[num+1]=NULL;p->curr_sentence=NULL;}}voidend_paragraph(PointersType*p){// Add the current paragraph to the document, making space for it - if there is a paragraphif(p->curr_para){intnum=paragraphs_in_document(p->curr_doc);p->curr_doc=realloc(p->curr_doc,(2+num)*sizeof(ParagraphType));p->curr_doc[num]=p->curr_para;p->curr_doc[num+1]=NULL;// Clear down current paragraphp->curr_para=NULL;}}DocumentTypeget_document(char*text){PointersTypedata={0};while(*text!='\0'){switch(*text){case'\n':// End of word, sentence and paragraph*text='\0';// Close off current wordend_word(&data);end_sentence(&data);end_paragraph(&data);break;case'.':// End of word and end of sentence. *text='\0';// Close off current wordend_word(&data);end_sentence(&data);break;case' ':// End of word*text='\0';// Close off current wordend_word(&data);break;default:if(!data.curr_word){// Start current word here since we have text which is not a special characterdata.curr_word=text;}break;}text++;}// Finish up any word, sentence and paragraph which was pendingend_word(&data);end_sentence(&data);end_paragraph(&data);returndata.curr_doc;// return byval}
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 →
Solution which uses a NULL pointer as a delimiter for the array of words/sentences/paragraphs so that the length of the array can be determined dynamically...