Structuring the Document

  • + 0 comments

    My solution which allocates the correct amount of space for each word, sentence and paragraph before populating the document.

    struct document get_document(char* text) {
        // Initialise variables.
        struct document Doc;
        int P = 1;
        int p, s, w, c;
        // Count the number of paragraphs.
        for (int i=0; i < strlen(text); i++) {
            if (text[i] == '\n') {
                P++;
            }
        }
        Doc.data = calloc(P, sizeof(struct paragraph));
        Doc.paragraph_count = P;
        for (int i=0; i < P; i++) {
            struct paragraph Para;
            Doc.data[i] = Para;
        }
        // Count the number of sentences in each paragraph.
        p = 0;
        s = 0;
        for (int i=0; i < strlen(text); i++) {
            if (text[i] == '\n') {
                Doc.data[p].data = calloc(s, sizeof(struct sentence));
                Doc.data[p].sentence_count = s;
                p++;
                s = 0;
            } else if (text[i] == '.') {
                s++;
            }
        }
        Doc.data[p].data = calloc(s, sizeof(struct sentence));
        Doc.data[p].sentence_count = s;
        // Count the number of words in each sentence in each paragraph.
        p = 0;
        s = 0;
        w = 0;
        for (int i=0; i < strlen(text); i++) {
            if (text[i] == '\n') {
                p++;
                s = 0;
            } else if (text[i] == '.') {
                w++;
                // printf("%d %d %d\n", p, s, w);
                Doc.data[p].data[s].data = calloc(w, sizeof(struct word));
                Doc.data[p].data[s].word_count = w;
                s++;
                w = 0;
            } else if (text[i] == ' ') {
                w++;
            }
        }
        // Count the number of characters in each word in each ... .
        p = 0;
        s = 0;
        w = 0;
        c = 0;
        for (int i=0; i < strlen(text); i++) {
            if (text[i] == '\n') {
                p++;
                s = 0;
            } else if (text[i] == '.') {
                Doc.data[p].data[s].data[w].data = calloc(c, sizeof(char));
                s++;
                w = 0;
                c = 0;
            } else if (text[i] == ' ') {
                Doc.data[p].data[s].data[w].data = calloc(c, sizeof(char));
                w++;
                c = 0;
            } else {
                c++;
            }
        }
        // Populate document.
        p = 0;
        s = 0;
        w = 0;
        c = 0;
        for (int i=0; i < strlen(text); i++) {
            if (text[i] == '\n') {
                p++;
                s = 0;
            } else if (text[i] == '.') {
                s++;
                w = 0;
                c = 0;
            } else if (text[i] == ' ') {
                w++;
                c = 0;
            } else {
                Doc.data[p].data[s].data[w].data[c] = text[i];
                c++;
            }
        }
        return Doc;
    }