View difference between Paste ID: 8TPgdWbK and 1dTgV10Q
SHOW: | | - or go back to the newest paste.
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <ctype.h>
5
void add_symbol(char **line, char symb, int len)
6
{
7
    int new_size = (len+2)*sizeof(char); //+1 for symbol, +2 for \0
8
    char *line_copy = (char*) malloc(new_size);
9
    strcpy(line_copy, *line);
10
    line_copy[len] = symb;
11
    line_copy[len+1] = '\0';
12
    *line = (char *) realloc(*line, new_size);
13
    strcpy(*line, line_copy);
14
    free(line_copy);
15
}
16
void add_row(char ***array, char *line, int rows_amount)
17
{
18
    *array = (char**)realloc(*array, sizeof(char*)*rows_amount+1);
19
    (*array)[rows_amount] = (char*) malloc(sizeof(char*)*100);
20
    strcpy((*array)[rows_amount], line);
21-
    while ((input = getchar()) != '0') //input != '0'
21+
22
int main()
23-
        if (isalpha(input))
23+
24
    char input;
25-
        else if (input != '\n') //\n, \r and other != EOF
25+
26-
            printf("Invalid input: %c\n", input); //
26+
27
28-
    printf("Final line: \"%s\"\n", final_line);
28+
    int size = 0;
29
    char **string_array = (char*) malloc(sizeof(char*)*size);
30
31
    while ((input = getchar()) != '0')
32
    {
33
        if (isalnum(input) || input == ' ')
34
            add_symbol(&final_line, input, strlen(final_line));
35
        else if (input == '\n'){
36
            add_row(&string_array, final_line, size);
37
            printf("New line: %s\n", final_line); //
38
            final_line[0] = '\0';
39
            size++;
40
        }else printf("Invalid input: %c\n", input);
41
    }
42
    free(final_line);
43
    int i;
44
    for (i = 0; i!=size; i++)
45
    {
46
        printf("String array [%i]: %s\n", i, string_array[i]);
47
        free(string_array[i]);
48
    }
49
    free(string_array);
50
    return 0;
51
}