adplus-dvertising
Querying the Document in C HackerRank Solution

Querying the Document in C – HackerRank Solution

Querying the Document in C – HackerRank Solution Explain VIDEO :

Querying the Document in C – HackerRank Solution

Problem :

  • Objective
  • Task
  • Example
  • Input Format
  • Sample Input 0
  • Sample Output 0
  • Solution
  • Explanations

Objective :

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:

  • A word is described by¬†char*¬†.
  • A sentence is described by¬†char**. The words in the sentence are separated by one space (” “). The last word does not end with a space(” “).
  • A paragraph is described by¬†char***. The sentences in the paragraph are separated by one period (“.”).
  • A document is described by¬†char****. The paragraphs in the document are separated by one newline(“\n”). The last paragraph does not end with a newline.
For example:
Learning C is fun.
Learning pointers is more fun.It is good to have pointers.

The only sentence in the first paragraph could be represented as:
char** first_sentence_in_first_paragraph = {“Learning”, “C”, “is”, “fun”};

The first paragraph itself could be represented as:
char*** first_paragraph = {{“Learning”, “C”, “is”, “fun”}};

The first sentence in the second paragraph could be represented as:
char** first_sentence_in_second_paragraph = {“Learning”, “pointers”, “is”, “more”, “fun”};

The second sentence in the second paragraph could be represented as:
char** second_sentence_in_second_paragraph = {“It”, “is”, “good”, “to”, “have”, “pointers”};

The second paragraph could be represented as:

char*** second_paragraph = {{“Learning”, “pointers”, “is”, “more”, “fun”}, {“It”, “is”, “good”, “to”, “have”, “pointers”}};

char**** document = {{{“Learning”, “C”, “is”, “fun”}}, {{“Learning”, “pointers”, “is”, “more”, “fun”}, {“It”, “is”, “good”, “to”, “have”, “pointers”}}};

Alicia has sent a document to her friend Teodora as a string of characters, i.e. represented by char* not char****. Help her convert the document to char**** form by completing the following functions:
char**** get_document(char* text) to return the document represented by char****.
char*** kth_paragraph(char**** document, int k) to return the kth Paragraph
char** kth_sentence_in_mth_paragraph(char**** document, int k, int m) to return the kth sentence in the mth paragraph.
char* kth_word_in_mth_sentence_of_nth_paragraph(char**** document, int k, int m, int n) to return the kth word in the mth sentence of the nth paragraph.

 

Task:

Learning C is fun.
Learning pointers is more fun.It is good to have pointers.

Input Format:

The first line contains the integer paragraph_count.
Each of the next paragraph_count lines contains a paragraph as a single string.
The next line contains the integer q, the number of queries.

Each of the next q lines or groups of lines contains a query in one of the following formats:
  • 1 The first line contains¬†1 k:
    • The next line contains an integer¬†x, the number of sentences in the¬†kth¬†paragraph.
    • Each of the next¬†x¬†lines contains an integer¬†a[i], the number of words in the¬†ith¬†sentence.
    • This query corresponds to calling the function kth_paragraph.
  • 2 The first line contains¬†2 k m:
    • The next line contains an integer¬†x, the number of words in the¬†kth¬†sentence of the¬†mth¬†paragraph.
    • This query corresponds to calling the function¬†kth_sentence_in_mth_paragraph
  • 3 The only line contains¬†3 k m n:
    • This query corresponds to calling the function¬†kth_word_in_mth_sentence_of_nth_paragraph

Sample Input 0:

2
Learning C is fun.
Learning pointers is more fun.It is good to have pointers.
3
1 2
2
5
6
2 1 1
4
3 1 1 1

Sample Output 0:

Learning pointers is more fun.It is good to have pointers.
Learning C is fun
Learning

Querying the Document in C – HackerRank Solution

Solution Solution :

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];
}

char*** kth_paragraph(char**** document, int k) {
return document[k – 1];
}

char**** get_document(char* text) {
// doc points to an array of paragraphs
char ****doc = malloc(MAX_PARAGRAPHS * sizeof(char ***));

// for each paragraph, assign an array of 1 sentence, we can reallocate later
for(int i = 0; i < MAX_PARAGRAPHS; i ++) {
doc[i] = malloc(1 * sizeof(char **));
}

// for each sentence assign an array of 1 word
for(int i = 0; i < MAX_PARAGRAPHS; i ++) {
for(int j = 0; j < 1; j ++) {
doc[i][j] = malloc(1 * sizeof(char*));
}
}

// for each word assign an array of 1 character
for(int i = 0; i < MAX_PARAGRAPHS; i ++) {
for(int j = 0; j < 1; j ++) {
for(int k = 0; k < 1; k ++) {
doc[i][j][k] = malloc(1 * sizeof(char));
}
}
}

for(int n = 0, i = 0, j = 0, k = 0, l = 0; n < strlen(text); n ++) {
if(text[n] != ‘ ‘ && text[n] != ‘\n’ && text[n] != ‘.’) {
doc[i][j][k][l] = text[n];
l++;
doc[i][j][k] = realloc(doc[i][j][k], (l + 1) * sizeof(char));
} else if(text[n] == ‘ ‘) {
doc[i][j][k][l] = ‘\0’;
l = 0;
k++;
doc[i][j] = realloc(doc[i][j], (k + 1) * sizeof(char*));
doc[i][j][k] = malloc(sizeof(char));
continue;
} else if(text[n] == ‘.’) {
doc[i][j][k][l] = ‘\0’;
k = l = 0;
j++;
doc[i] = realloc(doc[i], (j+1) * sizeof(char**));
doc[i][j] = malloc(sizeof(char*));
doc[i][j][k] = malloc(sizeof(char));
continue;
} else if(text[n] == ‘\n’) {
j = k = l = 0;
i++;
continue;
}
}
return doc;
}

Do Solve in Hackerrank – Click Here

Explanations : Click Me To Get Explain Video

Firstly, You have to Copy The Code Here and Go to Your Querying the Document in C РHackerRank Solution Questions Problem. Then You Have to go Submission Page. Now You Have Pasted this Copy Code and Run This Code by Compiler. You Can See All Task Should be Done. All Private and Public Cases Passed. Then Submit Your Code Finally.

What is #Include<stdio.h> ?

#include<stdio.h> is used to included header file in c programming language. It is a Mandetory. It is also Compiler Directives to Include INPUT/OUTPUT related function in our Program. The¬†stdio.h¬†is a file with “.h” extension which is contain the prototypes [not definition] of standard input-output functions used in c program.

For Others File We Have to Discuss in Future Articles. Visit Our Official Website.

Querying the Document in C РHackerRank Solution is a Solution Where You Can See The Code of Querying the Document in C РHackerRank Solution and Understand The Code Level. If You Want Others Code of Hackerrank Solution In C then You Can Visit Our Official Website For More Information About Hackerrank Solution In C.

What is int main() ?

‚Äėint main‚Äô means our perform must come thusme whole number at the top of the execution and that we do so by returning zero at the top of the program. zero is that the normal for the ‚Äúsuccessful execution of the program‚ÄĚ.

Also You Can Read – Database Management System (DBMS)

What is Printf and Scanf ?

The printf() and scanf() functions area unit used for input and output in C language. each functions area unit inherent library functions, outlined in stdio.h (header file).

What is Compiler ?

The language processor that reads the whole program written in application-oriented language as a full in one go and interprets it into identical program in machine language is named a Compiler. Example: C, C++, C#, Java. compilers/assemblers area unit themselves package, and reside where they were put in on the pc. that additionally implies that you just will have as many/few of every as you wish.

How “Querying the Document in C – HackerRank Solution” program works?

  • The #include is a preprocessor command that tells the compiler to include the contents of stdio.h (standard input and output) file in the program.
  • The stdio.h file contains functions such as scanf() and printf() to take input and display output respectively.
  • If you use the printf() function without writing #include <stdio.h>, the program will not compile.
  • The execution of a C program starts from the main() function.
  • printf() is a library function to send formatted output to the screen. In this program, printf() displays Hello, World! in C text on the screen.
  • The¬†return 0;¬†statement is the¬†“Exit status”¬†of the program. In simple terms, the program ends with this statement.

The #include could be a preprocessor command that tells the compiler to incorporate the contents of stdio.h (standard input and output) enter the program.
The stdio.h file contains functions like scanf() and printf() to require input and show output severally.
If you utilize the printf() perform while not writing #include , the program won’t compile.
The execution of a program starts from the main() perform.
printf() could be a library perform to send formatted output to the screen. during this program, printf() displays hello, World! in C text on the screen.
The¬†return 0;¬†statement is the¬†“Exit status” of the program. In straightforward terms, the program ends with this statement.

Hackerrank Answer Querying the Document in C – HackerRank Solution

Querying the Document in C – HackerRank Solution may be a sample program designed to acquaint users with most programming languages. Beginners ar introduced to the essential syntax of a programing language by learning the way to print out “Hello World” on the device screen. printf(“Hello World”); This line tells the compiler to show the message “Hello World” on the screen. This line is termed an announcement in C. each statement is supposed to perform some task.

Conclusion :

So, Learners today you have learned about basic Querying the Document in C РHackerRank Solution . Now you can solve Querying the Document in C РHackerRank Problem.

If you still have any doubt regarding the problem Just Watch Explanation Video, then Till any Queries , feel free to contact in the comment section.

Disclaimer:

The above problem Querying the Document in C РHackerRank Solution is generated by HackerRank but the solution is provided by Us (Check DMCA for Others). If you continue to have any doubt relating to the matter simply Watch rationalization Video, then until any Queries , be happy to contact within the comment section.

Related Posts

One thought on “Querying the Document in C – HackerRank Solution

  1. hello!,I really like your writing so a lot! percentage we
    communicate extra about your article on AOL?
    I require an expert in this space to unravel my problem.
    May be that is you! Looking ahead to look you.

Leave a Reply

Your email address will not be published.