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.
#define DEFAULT_LEN 8/// @brief Helper function to insert a character into a word, resizing the word as needed/// @param word pointer to pointer to char/// @param word_len pointer to int/// @param ch the charactervoidinsert_char(char**word,int*word_len,charch){/* These lines are important, also the '()' more important */(*word_len)++;(*word)=realloc(*word,sizeof(char)*(*word_len));(*word)[*word_len-1]=ch;}/// @brief Function to check if a character is whitespace/// @param ch the character/// @return booleanintis_whitespace(charch){return(ch==' ');}/// @brief Function to trim leading whitespace characters/// @param text pointer to char/// @param character pointer to intvoidtrim_whitespace(char*text,int*character){while(is_whitespace(text[*character])){(*character)++;}}/// @brief Function to check if a character is a valid text character/// @param ch the character/// @return booleanintis_text(charch){return(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z');}/// @brief Function to check if a character is a sentence terminator (period)/// @param ch the character/// @return booleanintis_sentence_terminator(charch){return(ch=='.');}/// @brief Function to check if a character is a paragraph terminator (newline or null terminator)/// @param ch the character/// @return booleanintis_paragraph_terminator(charch){return((ch=='\n')||(ch=='\0'));}/// @brief Function to get the next character in the text and increment the index/// @param text pointer to char/// @param character pointer to int/// @return **char** - the charactercharnext_character(char*text,int*character){return(text[(*character)++]);}/// @brief Function to parse the next word from the text/// @param text pointer to char/// @param character pointer to int/// @return word struct - the wordstructwordnext_word(char*text,int*character){char*word=malloc(DEFAULT_LEN*sizeof(char));// Allocate initial memory for the wordintword_size=0;// Initialize word size// Loop through characters until a non-text character is encounteredwhile(is_text(text[*character])){charch=next_character(text,character);// Get the next characterinsert_char(&word,&word_size,ch);// Insert the character into the word}insert_char(&word,&word_size,'\0');// Null-terminate the word// Move word array to the data array of the word structurestructwordW;W.data=word;returnW;}/// @brief Function to parse the next sentence from the text/// @param text pointer to char/// @param character pointer to int/// @return sentence struct - the sentencestructsentencenext_sentence(char*text,int*character){structsentenceS;S.data=malloc(DEFAULT_LEN*sizeof(structword));// Allocate initial memory for the sentenceS.word_count=0;// Initialize sentence length// Loop through characters until a sentence terminator is encounteredwhile(!is_sentence_terminator(text[*character])){trim_whitespace(text,character);// Trim leading whitespacestructwordW=next_word(text,character);// Parse the next wordS.word_count++;S.data=realloc(S.data,S.word_count*sizeof(structword));// Resize the sentence arrayS.data[S.word_count-1]=W;// Add the word to the sentence}next_character(text,character);// Move past the periodreturnS;}/// @brief Function to parse the next paragraph from the text/// @param text pointer to char/// @param character pointer to int/// @return paragraph struct - the paragraphstructparagraphnext_paragraph(char*text,int*character){structparagraphP;P.data=malloc(DEFAULT_LEN*sizeof(structsentence));// Allocate initial memory for the paragraphP.sentence_count=0;// Initialize paragraph length// Loop through characters until a paragraph terminator is encounteredwhile(!is_paragraph_terminator(text[*character])){structsentenceS=next_sentence(text,character);// Parse the next sentenceP.sentence_count++;P.data=realloc(P.data,P.sentence_count*sizeof(structsentence));// Resize the paragraph arrayP.data[P.sentence_count-1]=S;// Add the sentence to the paragraph}returnP;}/// @brief Function to parse the entire document from the text/// @param text pointer to char/// @return document struct - the documentstructdocumentget_document(char*text){structdocumentD;D.data=malloc(DEFAULT_LEN*sizeof(structparagraph));// Allocate initial memory for the documentD.paragraph_count=0;// Initialize document lengthintcharacter=0;// Initialize character indexintlast_paragraph=0;// Flag to check if the last paragraph has been reached// Loop until the last paragraph is reachedwhile(!last_paragraph){structparagraphP=next_paragraph(text,&character);// Parse the next paragraphlast_paragraph=next_character(text,&character)=='\0';// Check if the last character is reachedD.paragraph_count++;D.data=realloc(D.data,D.paragraph_count*sizeof(structparagraph));// Resize the document arrayD.data[D.paragraph_count-1]=P;// Add the paragraph to the document}returnD;}/// @brief Return the k-th word in the m-th sentence of the n-th paragraph/// @param document the document/// @param k the words/// @param m the sentences/// @param n the paragraphs/// @return word structstructwordkth_word_in_mth_sentence_of_nth_paragraph(structdocumentDoc,intk,intm,intn){returnDoc.data[n-1].data[m-1].data[k-1];}/// @brief Return the k-th sentence in the m-th paragraph/// @param document the document/// @param k the sentences/// @param m the paragraphs/// @return sentence structstructsentencekth_sentence_in_mth_paragraph(structdocumentDoc,intk,intm){returnDoc.data[m-1].data[k-1];}/// @brief Return the k-th paragraph/// @param document the document/// @param k the paragraphs/// @return paragraph structstructparagraphkth_paragraph(structdocumentDoc,intk){returnDoc.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 →
.c