So I have this piece of code:
void main()
{
char word[21]={0}; // changed from 20 to 21 because of '\0'.
// do {
scanf("%s", &word);
// } while (strlen(niz)>20); this line is useless because program crashes anyway
}
Question: The thing is I want to limit the number of characters user wants to input to 20. Sure I could reserve more memory for word[] but if I went over certain number the program would crash again. How do you get around this in C?
Question: Is there any way I can allocate needed memory for the string just after user enters it. For example if user enters string that has length of 999 can I allocate sizeof(string) * strlen(word) + 1 bytes of memory?
Note: if strlen() exceeds certain limit I want to prompt user to enter a new string again.