I want to make a program that stores inputed words into a 2D array (it can be as many as you want) and once you input word 'end', program should get out of the while loop. If I for example input these:
word1
word2
longer-word
end
Then the output should be:
List of words are:
word1
word2
longer-word
But here's the problem, I obviously dont know how many words will user input ( I dynamically allocated 2d array 5*20, so max 5 words), so what's the "correct" way of dynamically allocating 2D array, do I have to somehow reallocate every time user input new word or what? I really have no idea how to do it.
Here's a code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** allocate(int n, int m) {
    char** arr = (char**)malloc(sizeof(char*) * n);
    for (int i = 0; i < n; i++)
        arr[i] = (char*)malloc(sizeof(char) * m);
    return arr;
}
void print(char** words, int n, int m) {
    printf("List of words: \n");
    for (int i = 0; i < n; i++) {
        printf("%s\n", words[i]);
    }
}
int main() {
    char** words = allocate(5,20);
    char input[20];
    int index = 0;
    while ( strcmp("end", input) ) {
        scanf(" %s", input);
        if (strcmp("end", input) != 0) {
            strcpy(words[index], input);
            index++;
        }
    }
    
    print(words, 5, 20);
    return 0;
}
And also I noticed that when you input two words( with space ) it outputs those 2 words separately, so how can i prevent this?
 
     
     
    