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.
The split() function splits a string into an array of strings according to the 'delim' char argument and it updates the int pointer 'count' to the length of the array of strings. I used this function to do splits: document -> paragraphs, paragraphs -> sentences, sentences -> words.
I hope the reader finds the comments useful.
char*kth_word_in_mth_sentence_of_nth_paragraph(char****document,intk,intm,intn){return*(*(*(document+--n)+--m)+--k);}char**kth_sentence_in_mth_paragraph(char****document,intk,intm){return*(*(document+--m)+--k);}char***kth_paragraph(char****document,intk){return*(document+--k);}char**split(char*s,chardelim,int*count){char**split_s=NULL;char*part=NULL;unsignedintidx_split_s=0;unsignedintidx_part=0;charc='\0';for(unsignedinti=0;*(s+i)!='\0';i++){c=*(s+i);if(c==delim&&idx_part!=0){// mark the end of part stringpart=realloc(part,sizeof(char*)*(idx_part+1));*(part+idx_part)='\0';// append part to split_ssplit_s=realloc(split_s,sizeof(char**)*(idx_split_s+1));*(split_s+idx_split_s)=part;idx_split_s++;// clear part string valuespart=NULL;idx_part=0;}else{// append character to partpart=realloc(part,sizeof(char*)*(idx_part+1));*(part+idx_part)=c;idx_part++;}}// append last part to split_sif(idx_part!=0){// mark the end of part stringpart=realloc(part,sizeof(char*)*(idx_part+1));*(part+idx_part)='\0';// append part to split_ssplit_s=realloc(split_s,sizeof(char**)*(idx_split_s+1));*(split_s+idx_split_s)=part;idx_split_s++;}*count=idx_split_s;returnsplit_s;}char****get_document(char*text){int*len_paragraphs=calloc(1,sizeof(int));int*len_sentences=calloc(1,sizeof(int));int*len_words=calloc(1,sizeof(int));// split into paragraphs and store temporarilychar**paragraphs_temp=split(text,'\n',len_paragraphs);// allocate space for paragraphs in documentchar****document=malloc(*len_paragraphs*sizeof(char***));char*s=NULL;for(inti=0;i<*len_paragraphs;i++){s=*(paragraphs_temp+i);// get current paragraph// split into sentences and store temporarilychar**sentences_temp=split(s,'.',len_sentences);// allocate space for sentences in each paragraph*(document+i)=malloc(*len_sentences*sizeof(char**));for(intj=0;j<*len_sentences;j++){s=*(sentences_temp+j);// get crrent sentence// split into words and store temporarilychar**words_temp=split(s,' ',len_words);// allocate space for words in each sentence*(*(document+i)+j)=malloc(*len_words*sizeof(char*));for(intk=0;k<*len_words;k++){s=*(words_temp+k);// get current word*(*(*(document+i)+j)+k)=s;}// end of len_words loop}// end of len_sentences loop}// end of len_paragraphs loop// free stuff in heapfree(len_words);free(len_sentences);free(len_paragraphs);returndocument;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Querying the Document
You are viewing a single comment's thread. Return to all comments →
The split() function splits a string into an array of strings according to the 'delim' char argument and it updates the int pointer 'count' to the length of the array of strings. I used this function to do splits: document -> paragraphs, paragraphs -> sentences, sentences -> words.
I hope the reader finds the comments useful.