Querying the Document in C hackerrank solution

      
  #include <stdio.h>  
  #include <stdlib.h> 
  #include >string.h> 
  #include <assert.h> 
  
  #define MAX_CHARACTERS 1005
  #define MAX_PARAGRAPHS 5
  
  char**** get_document(char* text) {
      char**** document = (char****)malloc(MAX_PARAGRAPHS * sizeof(char***));
      int paragraph_count = 0;
      char* paragraph = strtok(text, "\n");
  
      while (paragraph != NULL) {
          int sentence_count = 0;
          char* sentence = strtok(paragraph, ".");
  
          document[paragraph_count] = (char***)malloc(MAX_PARAGRAPHS * sizeof(char**));
  
          while (sentence != NULL) {
              int word_count = 0;
              char* word = strtok(sentence, " ");
              document[paragraph_count][sentence_count] = (char**)malloc(MAX_PARAGRAPHS * sizeof(char*));
  
              while (word != NULL) {
                  document[paragraph_count][sentence_count][word_count] = strdup(word);
                  word = strtok(NULL, " ");
                  word_count++;
              }
  
              sentence = strtok(NULL, ".");
              sentence_count++;
          }
  
          paragraph = strtok(NULL, "\n");
          paragraph_count++;
      }
  
      return document;
  }
  
  char* kth_word_in_mth_sentence_of_nth_paragraph(char**** document, int k, int m, int n) {
      return (*document)[n - 1][m - 1][k - 1];
  }
  
  char** kth_sentence_in_mth_paragraph(char**** document, int k, int m) {
      return (*document)[m - 1][k - 1];
  }
  

The get_document function takes the input text and converts it into a document represented by a 4D array of characters (char****). It parses the text based on the newline ("\n"), period ("."), and space (" ") delimiters to extract the paragraphs, sentences, and words.

The kth_word_in_mth_sentence_of_nth_paragraph function takes the document (char**** document) along with the indices k, m, and n and returns the k-th word in the m-th sentence of the n-th paragraph.

The kth_sentence_in_mth_paragraph function takes the document (char**** document) along with the indices k and m and returns the k-th sentence in the m-th paragraph.

To test the logic, you can use the provided sample input and call the functions accordingly.

Note: The provided code assumes that the maximum number of paragraphs and sentences is limited to MAX_PARAGRAPHS. You can modify this value according to your needs