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.
Here is a solution I came up with. Pretty straightforward but not the most optimized.
structdocumentget_document(char*text){inti;//To be used to iterate when adding wordsintiIter=0;//General full length iteratorintiParaCount=1;//Count the paragraphs. Starts at 1 since last paragraph does not have \nintiWordCount=0;//Count words in each sentenceintiSentCount=0;//Count sentences in each paragraphsintiStart,iEnd;//Start and finish for word lengthintiWordSize;//Length of words extractedintiSentenceCount[MAX_PARAGRAPHS]={0,0,0,0,0};//Number of sentences in each paragraph//First iteration to get number of paragraphs and number of sentences in each paragraphwhile(text[iIter]!='\0'){if(text[iIter]=='.'){//End of sentenceiSentenceCount[iParaCount-1]++;}elseif(text[iIter]=='\n'){//End of paragraphiParaCount++;}iIter++;}structdocument*doc=(structdocument*)malloc(sizeof(structdocument));doc->paragraph_count=iParaCount;doc->data=(structparagraph*)malloc(iParaCount*sizeof(structparagraph));for(iIter=0;iIter<iParaCount;iIter++){doc->data[iIter].sentence_count=iSentenceCount[iIter];doc->data[iIter].data=(structsentence*)malloc(iSentenceCount[iIter]*sizeof(structsentence));}iEnd=0;iIter=0;iStart=0;iWordCount=0;iParaCount=0;iSentCount=0;while(text[iIter]!='\0'){if(text[iIter]==' '){//End of wordiWordCount++;}elseif(text[iIter]=='.'){//End of word AND sentenceiWordCount++;doc->data[iParaCount].data[iSentCount].word_count=iWordCount;doc->data[iParaCount].data[iSentCount].data=(structword*)malloc(iWordCount*sizeof(structword));//Iterate again from the beginning of the sentence to find words and save themfor(i=0;i<iWordCount;i++){iEnd=iStart;while((text[iEnd]!=' ')&&(text[iEnd]!='.')){//End of wordiEnd++;}iWordSize=iEnd-iStart;doc->data[iParaCount].data[iSentCount].data[i].data=(char*)malloc((iWordSize+1)*sizeof(char));//Plus 1 for null charactermemcpy(doc->data[iParaCount].data[iSentCount].data[i].data,&text[iStart],iWordSize*sizeof(char));doc->data[iParaCount].data[iSentCount].data[i].data[iWordSize]='\0';//Start is next of end. If new paragraph, add one moreiStart=iEnd+1;if(text[iStart]=='\n'){iStart++;}}iSentCount++;iWordCount=0;}elseif(text[iIter]=='\n'){iParaCount++;iSentCount=0;iWordCount=0;}iIter++;}return*doc;}structwordkth_word_in_mth_sentence_of_nth_paragraph(structdocumentDoc,intk,intm,intn){returnDoc.data[n-1].data[m-1].data[k-1];}structsentencekth_sentence_in_mth_paragraph(structdocumentDoc,intk,intm){returnDoc.data[m-1].data[k-1];}structparagraphkth_paragraph(structdocumentDoc,intk){returnDoc.data[k-1];}
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 →
Here is a solution I came up with. Pretty straightforward but not the most optimized.