-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_strings.c
More file actions
56 lines (41 loc) · 1.32 KB
/
dynamic_strings.c
File metadata and controls
56 lines (41 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*Bismillah*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[])
{
puts("\nEnter at most 10 characters");
puts("Enter \"end\" to exit\n");
char (*array)[10] = malloc(sizeof(char[10])); //allocating memory for the array of strings of 10 bt
if (array == NULL) {
perror("Error allocating memory");
exit(1);
}
char the_word[10];
int number = 0;
char (*temp_array)[10]; // a temp pointer variable to check when reallocating
while (1) {
fputs(">> ", stdout);
scanf("%s", the_word); //storing the word entered by the user in a temp array
if (strcmp(the_word, "end") == 0) { // checking if user is done with entering words
break;
}
strcpy(array[number], the_word); // copy the word to our array
++number;
temp_array = realloc(array, sizeof(char[10])*(number +1)); //number + 1, because we are staring from 0. and
//there already was space for 1 string. we need for 2
if (temp_array == NULL) {
perror("Error allocating memory");
exit(1);
}
array = temp_array;
}
puts("");
printf("You entered: %d words\n", number);
for (int i = 0; i < number ; i++) {
printf("<< %s\n", array[i]);
}
puts("");
return 0;
}