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.
My solution which allocates the correct amount of space for each word, sentence and paragraph before populating the document.
structdocumentget_document(char*text){// Initialise variables.structdocumentDoc;intP=1;intp,s,w,c;// Count the number of paragraphs.for(inti=0;i<strlen(text);i++){if(text[i]=='\n'){P++;}}Doc.data=calloc(P,sizeof(structparagraph));Doc.paragraph_count=P;for(inti=0;i<P;i++){structparagraphPara;Doc.data[i]=Para;}// Count the number of sentences in each paragraph.p=0;s=0;for(inti=0;i<strlen(text);i++){if(text[i]=='\n'){Doc.data[p].data=calloc(s,sizeof(structsentence));Doc.data[p].sentence_count=s;p++;s=0;}elseif(text[i]=='.'){s++;}}Doc.data[p].data=calloc(s,sizeof(structsentence));Doc.data[p].sentence_count=s;// Count the number of words in each sentence in each paragraph.p=0;s=0;w=0;for(inti=0;i<strlen(text);i++){if(text[i]=='\n'){p++;s=0;}elseif(text[i]=='.'){w++;// printf("%d %d %d\n", p, s, w);Doc.data[p].data[s].data=calloc(w,sizeof(structword));Doc.data[p].data[s].word_count=w;s++;w=0;}elseif(text[i]==' '){w++;}}// Count the number of characters in each word in each ... .p=0;s=0;w=0;c=0;for(inti=0;i<strlen(text);i++){if(text[i]=='\n'){p++;s=0;}elseif(text[i]=='.'){Doc.data[p].data[s].data[w].data=calloc(c,sizeof(char));s++;w=0;c=0;}elseif(text[i]==' '){Doc.data[p].data[s].data[w].data=calloc(c,sizeof(char));w++;c=0;}else{c++;}}// Populate document.p=0;s=0;w=0;c=0;for(inti=0;i<strlen(text);i++){if(text[i]=='\n'){p++;s=0;}elseif(text[i]=='.'){s++;w=0;c=0;}elseif(text[i]==' '){w++;c=0;}else{Doc.data[p].data[s].data[w].data[c]=text[i];c++;}}returnDoc;}
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 →
My solution which allocates the correct amount of space for each word, sentence and paragraph before populating the document.