0

I am trying to take this input from the user.

ARRAY [1,2,3,4,5,6]

and pass the numbers to an array but it's not working.

This is the part that I need help

else if (strncmp(input, "CONSTRUCT", 9) == 0)
        {
            printf("CONSTRUCT\n");
            // CONSTRUCT [value1,value2,value3,...,valueN]
            int i = 0;
            char *token;
            char *str = strdup(input);
            char **array = str_split(str, '[');
            char **array2 = str_split(array[1], ']');
            char **array3 = str_split(array2[0], ',');
            int array4[100];
            for (i = 0; i < 100; i++)
            {
                array4[i] = atoi(array3[i]);
            }
            for (i = 0; i < 100; i++)
            {
                printf("%d\n", array4[i]);
            }
            for (i = 0; i < 100; i++)
            {
                root = insert(root, array4[i]);
            }
            printf("\n");
        }

This is whole program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct node
{
    int data_element;
    struct node *left, *right;
};

struct node *new_node(int data_element)
{
    struct node *temp = (struct node *)malloc(sizeof(struct node)); // Allocating memory to the node
    temp->data_element = data_element;
    temp->left = temp->right = NULL;
    return temp;
}

void display(struct node *root) // A function for the inroder traversal of the binary tree
{
    if (root != NULL)
    {
        display(root->left);
        printf("%d \n", root->data_element);
        display(root->right);
    }
}

struct node *insert(struct node *node, int data_element) // Function to insert a new node
{

    if (node == NULL)
        return new_node(data_element); // Return a new node if the tree if empty
    if (data_element < node->data_element)
    {
        node->left = insert(node->left, data_element);
    }
    else if (data_element > node->data_element)
    {
        node->right = insert(node->right, data_element);
    }
    else if (data_element == node->data_element)
    {
        node->left = insert(node->left, data_element);
    }
    return node;
}

// DELETE
struct node *minValueNode(struct node *node)
{
    struct node *current = node;
    while (current->left != NULL)
        current = current->left;
    return current;
}

struct node *Delete(struct node *root, int data_element)
{
    if (root == NULL)
        return root;

    if (data_element < root->data_element)
        root->left = Delete(root->left, data_element);
    else if (data_element > root->data_element)
        root->right = Delete(root->right, data_element);
    else
    {
        if (root->left == NULL)
        {
            struct node *temp = root->right;
            free(root);
            return temp;
        }
        else if (root->right == NULL)
        {
            struct node *temp = root->left;
            free(root);
            return temp;
        }

        struct node *temp = minValueNode(root->right);
        root->data_element = temp->data_element;
        root->right = Delete(root->right, temp->data_element);
    }
    return root;
}

// PARENT
struct node *parent(struct node *root, int data_element)
{
    if (root == NULL)
        return NULL;
    if (root->data_element == data_element)
        return NULL;
    if (root->data_element > data_element)
        return parent(root->left, data_element);
    else
        return parent(root->right, data_element);
}

char **str_split(char *a_str, const char a_delim)
{
    char **result = 0;
    size_t count = 0;
    char *tmp = a_str;
    char *last_comma = 0;
    char delim[2];
    delim[0] = a_delim;
    delim[1] = 0;

    /* Count how many elements will be extracted. */
    while (*tmp)
    {
        if (a_delim == *tmp)
        {
            count++;
            last_comma = tmp;
        }
        tmp++;
    }

    /* Add space for trailing token. */
    count += last_comma < (a_str + strlen(a_str) - 1);

    /* Add space for terminating null string so caller
       knows where the list of returned strings ends. */
    count++;

    result = malloc(sizeof(char *) * count);

    if (result)
    {
        size_t idx = 0;
        char *token = strtok(a_str, delim);

        while (token)
        {
            assert(idx < count);
            *(result + idx++) = strdup(token);
            token = strtok(0, delim);
        }
        assert(idx == count - 1);
        *(result + idx) = 0;
    }

    return result;
}

// take a string like this CONSTRUCT [31,65,3,10,5,100,3,12] return a array of int

int main()
{
    struct node *root = NULL;
    root = insert(root, 31);
    root = insert(root, 65);
    root = insert(root, 3);
    root = insert(root, 10);
    root = insert(root, 5);
    root = insert(root, 100);
    root = insert(root, 3);
    root = insert(root, 12);

    while (1)
    {
        char input[50];

        scanf("%s", input);

        if (strncmp(input, "EXIT", 4) == 0)
        {
            exit(0);
        }
        else if (strncmp(input, "CONSTRUCT", 9) == 0)
        {
            printf("CONSTRUCT\n");
            // CONSTRUCT [value1,value2,value3,...,valueN]
            int i = 0;
            char *token;
            char *str = strdup(input);
            char **array = str_split(str, '[');
            char **array2 = str_split(array[1], ']');
            char **array3 = str_split(array2[0], ',');
            int array4[100];
            for (i = 0; i < 100; i++)
            {
                array4[i] = atoi(array3[i]);
            }
            for (i = 0; i < 100; i++)
            {
                printf("%d\n", array4[i]);
            }
            for (i = 0; i < 100; i++)
            {
                root = insert(root, array4[i]);
            }
            printf("\n");
        }
        else if (strncmp(input, "INSERT", 6) == 0)
        {
            printf("INSERT\n");
        }
        else if (strncmp(input, "LIST", 4) == 0)
        {
            printf("LIST\n");
            display(root);
        }
        else if (strncmp(input, "PARENT", 6) == 0)
        {
            // PARENT 12
            printf("PARENT\n");
            char *token;
            token = strtok(input, " ");
            token = strtok(NULL, " ");
            int data_element = atoi(token);
            struct node *temp = parent(root, data_element);
            if (temp == NULL)
            {
                printf("NULL\n");
            }
            else
            {
                printf("%d\n", temp->data_element);
            }
        }
        else if (strncmp(input, "DELETE", 6) == 0)
        {
        }
    }
}

user438383
  • 4,338
  • 6
  • 23
  • 35
  • 1
    You forgot to post your code. – Cheatah May 01 '22 at 16:50
  • It's not the code its the string coming from the user I want to take that numbers into an array – Nightvision May 01 '22 at 16:51
  • 1
    Well... you can't do anything without code :-) – pmg May 01 '22 at 16:52
  • Okay I will post my code – Nightvision May 01 '22 at 16:52
  • SO is not a code writing service. TRy something, when you get stuck post the code you have and explain what problem you are hitting – pm100 May 01 '22 at 16:55
  • I added my code – Nightvision May 01 '22 at 16:58
  • ok, so whats the problem? (Apart from the fact that this code looks for CONSTRUCT but your input has ARRAY). BTW, without seeing a whole program its unlikely SO can help very much (what is 'insert', 'str_split'....) – pm100 May 01 '22 at 17:02
  • That's the part where I need help I just want to take input like "CONSTRUCT [1,2,3]" and have an array like [1,2,3]. I can do the rest – Nightvision May 01 '22 at 17:04
  • Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives. This also allows other people to easily test your program, by simply using copy&paste. – Andreas Wenzel May 01 '22 at 17:14
  • If this question were asked on javascript I would explain how to split that part containing the array and how to iterate over it and put values to a real array of integers but you guys just making things harder. Why do you need to see the whole program? – Nightvision May 01 '22 at 17:14
  • @Nightvision: We are not asking to see "the whole program". We are asking for a [mre]. – Andreas Wenzel May 01 '22 at 17:15
  • I'm pretty sure that part is a "minimal reproducible example". I just tried to save your guys time by just copying that part. – Nightvision May 01 '22 at 17:18
  • Okay, @AndreasWenzel I understand what you mean. You want an easy to test example. – Nightvision May 01 '22 at 17:22
  • is there a space after 'ARRAY' (or 'CONSTRUCT') – pm100 May 01 '22 at 17:28
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 01 '22 at 17:32
  • @pm100 yes there is one space – Nightvision May 01 '22 at 17:35
  • Guys, I solved it. When I take the input with gets(input), instead of scanf("%s",input) it worked! I think the problem is when we take input with scanf it has a new line on its end, I'm not sure but I remember something like that. – Nightvision May 01 '22 at 17:39
  • I suggest that you use [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead of `gets`. See this link for further information: [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/12149471) – Andreas Wenzel May 01 '22 at 17:51
  • @Nightvision: I am pleased that you found the information useful. I am curious, was there anything specific that you found useful? I posted several links on different topics, so I don't know which one you found useful. – Andreas Wenzel May 05 '22 at 23:52
  • The last one, i tried to execute my code on my schools assigment system and it said I can't use gets method because its dangerous. And I remembered your comment and fix it. Thank you! – Nightvision May 06 '22 at 00:05
  • @Nightvision: I assume that your previous comment was intended as a reply to me, but you did not write the comment in such a way that I was notified of your comment. Therefore, I only noticed your reply by coincidence. If you want people to which you are replying to receive a notification of your reply, you must use the `@` syntax with that person's name. Press the `Help` button when writing a comment for further information. If you do not notify the person you are replying to, then that person will likely not notice your comment. – Andreas Wenzel May 06 '22 at 00:44
  • @Nightvision: Only the owner of the post to which the comment is attached (which is you in this case) will automatically be notified of comments to that post. Therefore, you should generally use the `@` syntax when replying to posts, when the comment is attached to your question. – Andreas Wenzel May 06 '22 at 00:45

1 Answers1

1

here you simply run off the end of an array

       char** array3 = str_split(array2[0], ',');
        int array4[100];
        for (i = 0; i < 100; i++)
        {
            array4[i] = atoi(array3[i]);
        }

array3 is dynamically sized to number of numbers + 1, but you try to access 100 entries

you placed a null entry at the end of the list, use that

        int count = 0;
        for (i = 0; i < 100; i++)
        {
            if (array3[i] == NULL)
                break;
            count++;
            array4[i] = atoi(array3[i]);
        }
        for (i = 0; i < count; i++)
        {
            printf("%d\n", array4[i]);
        }
        for (i = 0; i < count; i++)
        {
            root = insert(root, array4[i]);
        }
      

I saw your comment about the space. this code does not work with a space after 'CONSTRUCT', thats because

  scanf("%s", input);

reads up to the first space - you want fgets.

Andreas Wenzel
  • 12,860
  • 3
  • 14
  • 29
pm100
  • 42,706
  • 22
  • 76
  • 135