Querying the Document

  • + 0 comments
    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) {
        char**** document = (char****)malloc(sizeof(char***)*5);
        for (int i = 0; i < 5; i++){
            document[i] = (char***)malloc(sizeof(char**)*10);
            for (int j = 0; j < 10; j++){
                document[i][j] = (char**)malloc(sizeof(char*)*50);
                for(int k = 0; k < 50; k++){
                    document[i][j][k] = (char*)malloc(sizeof(char)*100);
                }
            }
        } 
        char ch = ' ';
        int par = 0, sen = 0, wor = 0, let = 0;
        for (int i = 0; i < strlen(text); i++){
            ch = text[i];
            if (ch == ' '){
                wor++;
                let = 0;
            } else if (ch == '.'){
                sen++;
                wor = 0;
                let = 0;
            } else if (ch == '\n'){
                par++;
                sen = 0;
                wor = 0;
                let = 0;
            } else{
                document[par][sen][wor][let] = ch;
                let++;
            }
        }
        return document;
    }