HackerRank C- Querying the Document




You will convert a raw text document into its component paragraphs, sentences and words. To test your results, queries will ask you to return a specific paragraph, sentence or word as described below.

Alicia is studying the C programming language at the University of Dunkirk and she represents the words, sentences, paragraphs, and documents using pointers:



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include<assert.h>
  5. #define MAX_CHARACTERS 1005
  6. #define MAX_PARAGRAPHS 5
  7. char* kth_word_in_mth_sentence_of_nth_paragraph(char**** document, int k, int m, int n) {
  8. return document[n-1][m-1][k-1];
  9. }
  10. char** kth_sentence_in_mth_paragraph(char**** document, int k, int m) {
  11. return document[m-1][k-1];
  12. }
  13. char*** kth_paragraph(char**** document, int k) {
  14. return document[k-1];
  15. }
  16. char** split_string(char* text, char delim) {
  17. assert(text != NULL);
  18. char** result = malloc(1*sizeof(char*));
  19. int size = 1;
  20. char* temp = strtok(text, &delim);
  21. *result = temp;
  22. while(temp != NULL) {
  23. size++;
  24. result = realloc(result,size*sizeof(char*));
  25. temp = strtok(NULL, &delim);
  26. result[size-1] = temp;
  27. }
  28. return result;
  29. }
  30. char**** get_document(char* text) {
  31. assert(text != NULL);
  32. // split text by '\n' and count number of paragraphs
  33. char** paragraphs = split_string(text, '\n');
  34. int npar = 0;
  35. while (paragraphs[npar] != NULL) {
  36. npar++;
  37. }
  38. char**** doc = malloc((npar+1)*sizeof(char***));
  39. // set last position to NULL for the user
  40. // to know when the array ends.
  41. doc[npar] = NULL;
  42. int i = 0;
  43. while (paragraphs[i] != NULL) {
  44. // split sentences of paragraph by '.' and count number of sentences
  45. char** sentences = split_string(paragraphs[i], '.');
  46. int nsen = 0;
  47. while(sentences[nsen] != NULL) {
  48. nsen++;
  49. }
  50. doc[i] = malloc((nsen+1)*sizeof(char**));
  51. // set last position to NULL for the user
  52. // to know when the array ends.
  53. doc[i][nsen] = NULL;
  54. int j = 0;
  55. while (sentences[j] != NULL) {
  56. // remember that doc[0][0] means: paragraph #0,
  57. // sentence #0 and should act like a pointer to
  58. // the first element of an array of words (strings)
  59. // split string by ' ' and associate doc[i][j]
  60. // with the array of strings representing words
  61. // that is returned by split_string.
  62. doc[i][j] = split_string(sentences[j], ' ');
  63. j++;
  64. }
  65. i++;
  66. }
  67. return doc;
  68. }
  69. char* get_input_text() {
  70. int paragraph_count;
  71. scanf("%d", &paragraph_count);
  72. char p[MAX_PARAGRAPHS][MAX_CHARACTERS], doc[MAX_CHARACTERS];
  73. memset(doc, 0, sizeof(doc));
  74. getchar();
  75. for (int i = 0; i < paragraph_count; i++) {
  76. scanf("%[^\n]%*c", p[i]);
  77. strcat(doc, p[i]);
  78. if (i != paragraph_count - 1)
  79. strcat(doc, "\n");
  80. }
  81. char* returnDoc = (char*)malloc((strlen (doc)+1) * (sizeof(char)));
  82. strcpy(returnDoc, doc);
  83. return returnDoc;
  84. }
  85. void print_word(char* word) {
  86. printf("%s", word);
  87. }
  88. void print_sentence(char** sentence) {
  89. int word_count;
  90. scanf("%d", &word_count);
  91. for(int i = 0; i < word_count; i++){
  92. printf("%s", sentence[i]);
  93. if( i != word_count - 1)
  94. printf(" ");
  95. }
  96. }
  97. void print_paragraph(char*** paragraph) {
  98. int sentence_count;
  99. scanf("%d", &sentence_count);
  100. for (int i = 0; i < sentence_count; i++) {
  101. print_sentence(*(paragraph + i));
  102. printf(".");
  103. }
  104. }
  105. int main()
  106. {
  107. char* text = get_input_text();
  108. char**** document = get_document(text);
  109. int q;
  110. scanf("%d", &q);
  111. while (q--) {
  112. int type;
  113. scanf("%d", &type);
  114. if (type == 3){
  115. int k, m, n;
  116. scanf("%d %d %d", &k, &m, &n);
  117. char* word = kth_word_in_mth_sentence_of_nth_paragraph(document, k, m, n);
  118. print_word(word);
  119. }
  120. else if (type == 2){
  121. int k, m;
  122. scanf("%d %d", &k, &m);
  123. char** sentence = kth_sentence_in_mth_paragraph(document, k, m);
  124. print_sentence(sentence);
  125. }
  126. else{
  127. int k;
  128. scanf("%d", &k);
  129. char*** paragraph = kth_paragraph(document, k);
  130. print_paragraph(paragraph);
  131. }
  132. printf("\n");
  133. }
  134. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube