aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--simple-C-programs/DynamicString.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/simple-C-programs/DynamicString.c b/simple-C-programs/DynamicString.c
index 9141dba..c2dd729 100644
--- a/simple-C-programs/DynamicString.c
+++ b/simple-C-programs/DynamicString.c
@@ -14,12 +14,29 @@ int main() {
Dynamic_string = malloc(string_length);
int input;
+ int ch_count = 0;
while((input = getchar()) != '\n' && input != EOF) {
-
- }
+ Dynamic_string[ch_count] = input;
+ ch_count++;
+
+ if(ch_count > (string_length - 5)) {
+ string_length *= 2;
+ char *tmp = realloc(Dynamic_string, string_length);
+ if(!tmp) {
+ printf("Allocation failed!\n");
+ free(Dynamic_string);
+ return 0;
+ }
+ Dynamic_string = tmp;
+
+ }
+ }
+ Dynamic_string[ch_count] = '\0';
+ printf("%s\n", Dynamic_string);
+ free(Dynamic_string);
return 0;
} \ No newline at end of file