I am trying to make a program that I think should be simple. I want to ask for a word and store it in a vector with its right size (more \n logically). To do this, the program asks the user to write a word ending with a .. Then, the program reads letter by letter and stores the letters in a string that is created dynamically as the letters are being read.
int main() {
int i = 0, tam = 0;
char *cad = (char *)malloc(sizeof(char));
char c;
printf("word: ");
while (c != '.') {
scanf("%c", c);
cad[i] = c;
i++;
cad = realloc(cad, (i + 1) * sizeof(char));
}
cad[i] = '\0';
for (i = 0; cad[i] == '\0'; i++) {
tam++;
}
printf("tam: %d\n", tam);
return 0;
}
I have made this but I think that it doesn't do anything useful