[Solved] how can we write a program that can allow us to input as many string as we want?


#include <stdio.h>

#define MAX_STRING_COUNT 1000
#define MAX_STRING_LENGTH 100

int main()
{
    int n;
    char str[MAX_STRING_COUNT][MAX_STRING_LENGTH];
    int ret = scanf("%d\n",&n);
    if (ret != 1 || n < 0 || n > MAX_STRING_COUNT) {
        puts("Wrong number of strings!\n");
        return 1;
    }

    for(int i = 0; i < n; i++) {
        ret = fgets(str[i], MAX_STRING_LENGTH, stdin);
        if (ret == NULL) {
            puts("Error reading string\n");
            return 1;
        }
    }
    return 0;
}

Never read unbound strings. That may cause an overflow, that is security flow in your program.

8

solved how can we write a program that can allow us to input as many string as we want?