This is my code:
#include <stdio.h>
#include <string.h>
int main()
{
char Words[][20] = {"Dog", "Cat", "Table"};
char command[20];
do
{
printf("\nEnter command(Add, End): ");
/* input should be "Add" , write down a word, then "Add" again, then write down a new word. */
scanf("%s", &command);
if (strcmp(command, "Add") == 0)
{
int NumberOfWords = sizeof(Words) / sizeof(Words[0]);
char NewWord[20];
printf("Enter new word: ");
scanf("%s", &NewWord);
memcpy(Words[NumberOfWords], NewWord, sizeof(Words[NumberOfWords]));
printf("\nTEST- the number of words is: %d\n" , NumberOfWords);
}
}while (strcmp(command, "End") != 0);
printf("Program has ended!");
return 0;
}
The problem is, NumberOfWords
always stays 3 (AKA. the number of words is always 3) , even after supposedly adding value to Words[3]
(which should make the number of words = 4). And that makes adding more words into the array Words not possible.
How do I fix that?