I'm allocating memory of 10 bytes for string and reading the input char by char. If the input is longer I just reallocate another 10 bytes more. The input will be stopped by 'return'.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char c = 64;
// Allocating memory for a string of 10 bytes
char* str = (char*) malloc(10);
int i = 0;
// Reading input char by char
scanf("%c", &c);
// Stop if input is 'return'
while ( c != 10 ){
str[i]=c;
scanf("%c", &c);
// Reallocating 10 bytes more if neccessary
if ( i%10 == 0 ){
str = (char *) realloc(str, i+10);
}
i++;
}
str[i]='\0';
printf ("Input: %s\nLength: %ld\n", str, strlen (str));
return 0;
}