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<assert.h>#define MAX_CHARACTERS 1005#define MAX_PARAGRAPHS 5structword{char*data;};structsentence{structword*data;intword_count;//denotes number of words in a sentence};structparagraph{structsentence*data;intsentence_count;//denotes number of sentences in a paragraph};structdocument{structparagraph*data;intparagraph_count;//denotes number of paragraphs in a document};// returns: the current line of the stream// points stream to next linechar*getUntil(char**stream,chardelim){inti=0;while((*stream)[i]!=delim&&(*stream)[i]!='\0')i++;if((*stream)[i]=='\0'){return*stream;}(*stream)[i]='\0';char*ret=*stream;*stream=&((*stream)[i+1]);returnret;}// count number of delim characters in stringintnumerate(char*p,chardelim){intlen=strlen(p);intsum=0;for(inti=0;i<len;i++){if(p[i]==delim)sum++;}returnsum;}structdocumentget_document(char*text){structdocumentdoc;// initialize docdoc.paragraph_count=numerate(text,'\n')+1;doc.data=malloc(doc.paragraph_count*sizeof(structparagraph));char*currPg;char*currS;char*currW;// for each paragraph, count sentences and initialize sentencesfor(inti=0;i<doc.paragraph_count;i++){currPg=getUntil(&text,'\n');doc.data[i].sentence_count=numerate(currPg,'.');doc.data[i].data=malloc(doc.data[i].sentence_count*sizeof(structsentence));// for each sentence, count words and initialize wordsfor(intj=0;j<doc.data[i].sentence_count;j++){currS=getUntil(&currPg,'.');doc.data[i].data[j].word_count=numerate(currS,' ')+1;// plus one because last worddoc.data[i].data[j].data=malloc(doc.data[i].data[j].word_count*sizeof(structword));// for each word, initialize for(intk=0;k<doc.data[i].data[j].word_count;k++){currW=getUntil(&currS,' ');doc.data[i].data[j].data[k].data=currW;}}}returndoc;}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];}voidprint_word(structwordw){printf("%s",w.data);}voidprint_sentence(structsentencesen){for(inti=0;i<sen.word_count;i++){print_word(sen.data[i]);if(i!=sen.word_count-1){printf(" ");}}}voidprint_paragraph(structparagraphpara){for(inti=0;i<para.sentence_count;i++){print_sentence(para.data[i]);printf(".");}}voidprint_document(structdocumentdoc){for(inti=0;i<doc.paragraph_count;i++){print_paragraph(doc.data[i]);if(i!=doc.paragraph_count-1)printf("\n");}}char*get_input_text(){intparagraph_count;scanf("%d",¶graph_count);charp[MAX_PARAGRAPHS][MAX_CHARACTERS],doc[MAX_CHARACTERS];memset(doc,0,sizeof(doc));getchar();for(inti=0;i<paragraph_count;i++){scanf("%[^\n]%*c",p[i]);strcat(doc,p[i]);if(i!=paragraph_count-1)strcat(doc,"\n");}char*returnDoc=(char*)malloc((strlen(doc)+1)*(sizeof(char)));strcpy(returnDoc,doc);returnreturnDoc;}intmain(){char*text=get_input_text();structdocumentDoc=get_document(text);intq;scanf("%d",&q);while(q--){inttype;scanf("%d",&type);if(type==3){intk,m,n;scanf("%d %d %d",&k,&m,&n);structwordw=kth_word_in_mth_sentence_of_nth_paragraph(Doc,k,m,n);print_word(w);}elseif(type==2){intk,m;scanf("%d %d",&k,&m);structsentencesen=kth_sentence_in_mth_paragraph(Doc,k,m);print_sentence(sen);}else{intk;scanf("%d",&k);structparagraphpara=kth_paragraph(Doc,k);print_paragraph(para);}printf("\n");}}
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 →