Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- char *readline() {
- int max_size = 10;
- int size;
- char c;
- char *line;
- size = 0;
- line = malloc(sizeof(*line) * (max_size + 1));
- if (!line) {
- return NULL;
- }
- for (;;) {
- if (size == max_size) {
- max_size *= 2;
- line = realloc(line, sizeof(*line) * (max_size + 1));
- if (!line) {
- return NULL;
- }
- }
- c = getchar();
- if (c == EOF) {
- return EOF;
- }
- if (c == '\n') {
- break;
- }
- line[size++] = c;
- }
- line[size] = '\0';
- return line;
- }
- char **readlines(int *lines_count) {
- char **lines = malloc(sizeof(*lines) * 10);
- int size;
- char *line;
- size = 0;
- while ((line = readline()) != EOF) {
- lines[size++] = line;
- }
- *lines_count = size;
- return lines;
- }
- void printline(char *line) {
- char c;
- while ((c = *(line++)) != '\0') {
- putchar(c);
- }
- putchar('\n');
- }
- void printlines(char **lines, int lines_count) {
- for (int i = 0; i < lines_count; i++) {
- printline(lines[i]);
- }
- }
- int main() {
- int lines_count;
- char **lines;
- lines = readlines(&lines_count);
- printlines(lines, lines_count);
- free(lines);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement