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.
voidappend_word(structword*tmp_word,int*letter_count,charch){tmp_word->data=realloc(tmp_word->data,sizeof(char)*(*letter_count+1));tmp_word->data[*letter_count]=ch;*letter_count+=1;}voidreset_word(structword*tmp_word,int*letter_count){tmp_word->data=malloc(sizeof(char));*letter_count=0;}voidappend_sentence(structsentence*tmp_sentence,structword*tmp_word){tmp_sentence->data=realloc(tmp_sentence->data,sizeof(structword)*(tmp_sentence->word_count+1));tmp_sentence->data[tmp_sentence->word_count]=*tmp_word;tmp_sentence->word_count++;}voidreset_sentence(structsentence*tmp_sentence){tmp_sentence->data=malloc(sizeof(structword));tmp_sentence->word_count=0;}voidappend_paragraph(structparagraph*tmp_paragraph,structsentence*tmp_sentence){tmp_paragraph->data=realloc(tmp_paragraph->data,sizeof(structsentence)*(tmp_paragraph->sentence_count+1));tmp_paragraph->data[tmp_paragraph->sentence_count]=*tmp_sentence;tmp_paragraph->sentence_count++;}voidreset_paragraph(structparagraph*tmp_paragraph){tmp_paragraph->data=malloc(sizeof(structsentence));tmp_paragraph->sentence_count=0;}voidappend_document(structdocument*tmp_document,structparagraph*tmp_paragraph){tmp_document->data=realloc(tmp_document->data,sizeof(structparagraph)*(tmp_document->paragraph_count+1));tmp_document->data[tmp_document->paragraph_count]=*tmp_paragraph;tmp_document->paragraph_count++;}structdocumentget_document(char*text){//get length of textinttext_length=strlen(text);structword*tmp_word;intletters;tmp_word=malloc(sizeof(structword));reset_word(tmp_word,&letters);structsentence*tmp_sentence;tmp_sentence=malloc(sizeof(structsentence));reset_sentence(tmp_sentence);structparagraph*tmp_paragraph;tmp_paragraph=malloc(sizeof(structparagraph));reset_paragraph(tmp_paragraph);structdocument*tmp_document;tmp_document=malloc(sizeof(structdocument));tmp_document->paragraph_count=0;tmp_document->data=malloc(sizeof(structsentence));//paragraph[0]//iterate over the textfor(inti=0;i<text_length;i++){//condense by i<strlen(text)charch=text[i];if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){//found letter//put into wordappend_word(tmp_word,&letters,ch);}else{//found end of word if(letters>0){//terminate word stringtmp_word->data[letters]='\0';//put word into current sentence append_sentence(tmp_sentence,tmp_word);//clean tmp wordreset_word(tmp_word,&letters);}//check for end of sentence or paragraphif(ch==' '){//is word}elseif(ch=='\n'){//found end of paragraph//put into documentappend_document(tmp_document,tmp_paragraph);reset_paragraph(tmp_paragraph);}else{//found end of sentence//put into current paragraphappend_paragraph(tmp_paragraph,tmp_sentence);reset_sentence(tmp_sentence);}}}//end of documentappend_document(tmp_document,tmp_paragraph);return*tmp_document;}structparagraphkth_paragraph(structdocumentDoc,intk){returnDoc.data[k-1];}structsentencekth_sentence_in_mth_paragraph(structdocumentDoc,intk,intm){returnDoc.data[m-1].data[k-1];}structwordkth_word_in_mth_sentence_of_nth_paragraph(structdocumentDoc,intk,intm,intn){returnDoc.data[n-1].data[m-1].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 →
not the prettiest code i've ever written