HackerRank C- Structuring the Document




A document is represented as a collection paragraphs, a paragraph is represented as a collection of sentences, a sentence is represented as a collection of words and a word is represented as a collection of lower-case ([a-z]) and upper-case ([A-Z]) English characters. 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. struct word {
  8. char* data;
  9. };
  10. struct sentence {
  11. struct word* data;
  12. int word_count;//denotes number of words in a sentence
  13. };
  14. struct paragraph {
  15. struct sentence* data ;
  16. int sentence_count;//denotes number of sentences in a paragraph
  17. };
  18. struct document {
  19. struct paragraph* data;
  20. int paragraph_count;//denotes number of paragraphs in a document
  21. };
  22. struct document get_document(char* text) {
  23. struct document doc;
  24. doc.data = malloc(sizeof(struct paragraph) * 5);
  25. doc.paragraph_count = 0;
  26. for (int p=0; p<5; p++) {
  27. doc.data[p].data = malloc(sizeof(struct sentence) * 16);
  28. doc.data[p].sentence_count = 0;
  29. for (int s = 0; s < 16; s++) {
  30. doc.data[p].data[s].data = malloc(sizeof(struct word) * 128);
  31. doc.data[p].data[s].word_count = 0;
  32. for (int w = 0; w < 128; w++){
  33. doc.data[p].data[s].data[w].data = malloc(sizeof(char) * 256);
  34. }
  35. }
  36. }
  37. int p = 0, s = 0, w = 0, c = 0;
  38. for (int i = 0; i<strlen(text); i++) {
  39. if (text[i] == ' '){
  40. doc.data[p].data[s].data[w].data[c] = '\0';
  41. doc.data[p].data[s].word_count++;
  42. c = 0;
  43. w++;
  44. continue;
  45. }
  46. if (text[i] == '.'){
  47. doc.data[p].data[s].data[w].data[c] = '\0';
  48. doc.data[p].data[s].word_count++;
  49. doc.data[p].sentence_count++;
  50. c = 0; w = 0;
  51. s++;
  52. continue;
  53. }
  54. if (text[i] == '\n'){
  55. doc.paragraph_count++;
  56. c = 0; w = 0; s = 0;
  57. p++;
  58. continue;;
  59. }
  60. doc.data[p].data[s].data[w].data[c++] = text[i];
  61. }
  62. return doc;
  63. }
  64. struct word kth_word_in_mth_sentence_of_nth_paragraph(struct document Doc, int k, int m, int n) {
  65. return Doc.data[n-1].data[m-1].data[k-1];
  66. }
  67. struct sentence kth_sentence_in_mth_paragraph(struct document Doc, int k, int m) {
  68. return Doc.data[m-1].data[k-1];
  69. }
  70. struct paragraph kth_paragraph(struct document Doc, int k) {
  71. return Doc.data[k-1];
  72. }
  73. void print_word(struct word w) {
  74. printf("%s", w.data);
  75. }
  76. void print_sentence(struct sentence sen) {
  77. for(int i = 0; i < sen.word_count; i++) {
  78. print_word(sen.data[i]);
  79. if (i != sen.word_count - 1) {
  80. printf(" ");
  81. }
  82. }
  83. }
  84. void print_paragraph(struct paragraph para) {
  85. for(int i = 0; i < para.sentence_count; i++){
  86. print_sentence(para.data[i]);
  87. printf(".");
  88. }
  89. }
  90. void print_document(struct document doc) {
  91. for(int i = 0; i < doc.paragraph_count; i++) {
  92. print_paragraph(doc.data[i]);
  93. if (i != doc.paragraph_count - 1)
  94. printf("\n");
  95. }
  96. }
  97. char* get_input_text() {
  98. int paragraph_count;
  99. scanf("%d", &paragraph_count);
  100. char p[MAX_PARAGRAPHS][MAX_CHARACTERS], doc[MAX_CHARACTERS];
  101. memset(doc, 0, sizeof(doc));
  102. getchar();
  103. for (int i = 0; i < paragraph_count; i++) {
  104. scanf("%[^\n]%*c", p[i]);
  105. strcat(doc, p[i]);
  106. if (i != paragraph_count - 1)
  107. strcat(doc, "\n");
  108. }
  109. char* returnDoc = (char*)malloc((strlen (doc)+1) * (sizeof(char)));
  110. strcpy(returnDoc, doc);
  111. return returnDoc;
  112. }
  113. int main()
  114. {
  115. char* text = get_input_text();
  116. struct document Doc = get_document(text);
  117. int q;
  118. scanf("%d", &q);
  119. while (q--) {
  120. int type;
  121. scanf("%d", &type);
  122. if (type == 3){
  123. int k, m, n;
  124. scanf("%d %d %d", &k, &m, &n);
  125. struct word w = kth_word_in_mth_sentence_of_nth_paragraph(Doc, k, m, n);
  126. print_word(w);
  127. }
  128. else if (type == 2) {
  129. int k, m;
  130. scanf("%d %d", &k, &m);
  131. struct sentence sen= kth_sentence_in_mth_paragraph(Doc, k, m);
  132. print_sentence(sen);
  133. }
  134. else{
  135. int k;
  136. scanf("%d", &k);
  137. struct paragraph para = kth_paragraph(Doc, k);
  138. print_paragraph(para);
  139. }
  140. printf("\n");
  141. }
  142. }




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