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};structwordget_word(char*text,intbeg,intend){structwordanswer;answer.data=calloc(end-beg+1,sizeof(char));intidex=0;for(inti=beg;i<=end;i++){answer.data[idex]=text[i];++idex;}answer.data[idex]=0;returnanswer;}structsentenceget_sentence(char*text,intbeg,intend){structsentenceanswer;answer.word_count=1;inti;for(i=beg;i<=end;i++){if(text[i]==' '){answer.word_count++;}}answer.data=calloc(answer.word_count,sizeof(structword));intstart=beg;intidex=0;for(i=beg;i<=end;i++){if(text[i]==' '){answer.data[idex++]=get_word(text,start,i-1);start=i+1;}}answer.data[idex]=get_word(text,start,i-1);returnanswer;}structparagraphget_paragraph(char*text,intbeg,intend){structparagraphanswer;answer.sentence_count=0;inti;for(i=beg;i<=end;i++){if(text[i]=='.')answer.sentence_count++;}answer.data=calloc(answer.sentence_count,sizeof(structsentence));intstart=beg;intidex=0;for(i=beg;i<=end;i++){if(text[i]=='.'){answer.data[idex++]=get_sentence(text,start,i-1);start=i+1;}}returnanswer;}structdocumentget_document(char*text){structdocumentanswer;answer.paragraph_count=1;inti;for(i=0;i<strlen(text);i++){if(text[i]=='\n'){answer.paragraph_count++;}}answer.data=calloc(answer.paragraph_count,sizeof(structparagraph));intstart=0,idex=0;for(i=0;i<strlen(text);i++){if(text[i]=='\n'){answer.data[idex++]=get_paragraph(text,start,i-1);start=i+1;}}answer.data[idex]=get_paragraph(text,start,i-1);returnanswer;}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 →