首页 > 解决方案 > Structs and arrays challenge in C

问题描述

I am trying to solve this challenge: https://www.hackerrank.com/challenges/structuring-the-document/problem

Basically I have been given a locked stub of code with structs in it and I am supposed to parse a given text. This is an abridged version of my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX_CHARACTERS 1005
#define MAX_PARAGRAPHS 5

#include <ctype.h>

struct word {
    char* data;
};

struct sentence {
    struct word* data;
    int word_count;//denotes number of words in a sentence
};

struct paragraph {
    struct sentence* data  ;
    int sentence_count;//denotes number of sentences in a paragraph
};

struct document {
    struct paragraph* data;
    int paragraph_count;//denotes number of paragraphs in a document
};

struct document get_document(char* text) {
    int spaces = 0, periods = 0, newlines = 0;
    for(int i = 0; i < strlen(text); i++) 
        if(text[i] == ' ')
            spaces++;
        else if(text[i] == '.')
            periods++;
        else if(text[i] == '\n')
            newlines++;


    struct document doc;
    doc.paragraph_count = newlines + 1;
    doc.data = malloc((newlines + 1) * sizeof(struct paragraph));

    struct paragraph para[doc.paragraph_count];
    for(int i = 0; i < doc.paragraph_count; i++) {
        para[i].sentence_count = periods + 1;
        para[i].data = malloc((periods + 1) * sizeof(struct sentence));
    }

    struct sentence sen[para[0].sentence_count];
    for(int i = 0; i < para[0].sentence_count; i++) {
        sen[i].word_count = spaces + 1;
        sen[i].data = malloc((spaces + 1) * sizeof(struct word));
    }

    struct word word[spaces + periods + 1];

    int start = 0, k = 0, wordsub = 0, sensub = 0, parasub = 0, docsub = 0, wordno = 0, parano = 0;
    for(int i = 0; i < strlen(text); i++) {
        if(text[i] == ' ' || text[i] == '.') {
            word[wordsub].data = malloc((i - start) * sizeof(char) + 1);
            for(int j = start; j < i; j++)
                word[wordsub].data[k++] = text[j];
            word[wordsub].data[k++] = '\0';

            k = 0;

            if(i < strlen(text) - 1 && text[i + 1] == '\n')
                start = i + 2;
            else 
                start = i + 1;

            if(text[i] == ' ') {
                sen[sensub].data[wordno++] = word[wordsub++]; //wordno can be 0 or 1
            }
            if(i != strlen(text) && isalpha(text[i + 1]) && text[i] == '.') {
                sen[sensub].data[wordno++] = word[wordsub++];
                wordno = 0;
                para[parasub].data[parano++] = sen[sensub++];


            }
            if((i != strlen(text) && text[i + 1] == '\n') || i + 1 == strlen(text)) {
                sen[sensub++].data[wordno++] = word[wordsub];
                wordno = 0;

                parano = 0;
                para[parasub].data[parano++] = sen[sensub];

                doc.data[docsub++] = para[parasub++];


            }

        }
    }
    printf("%s\n", para[0].data[0].data[0].data);// should print "hello"
    return doc;
}

int main() {
    struct document doc;
    char * text = "hello world.\nhi.bye.\nwow.";
    doc = get_document(text);
    printf("%s\n", doc.data[0].data[0].data[0].data);//should also print "hello"
}

The problem is the print statements are not printing "hello". Also if I change the indices in the print statements I get a segmentation error.

标签: carraysstruct

解决方案


Here:

word[wordsub].data[k++] = text[j];

you are accessing data member out of allocated memory.


推荐阅读