Skip to content

Instantly share code, notes, and snippets.

@vporoshok
Created May 15, 2014 08:22
Show Gist options
  • Save vporoshok/165908369029a8b57ea2 to your computer and use it in GitHub Desktop.
Save vporoshok/165908369029a8b57ea2 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int mkdstr(char **, int (*)(void));
int main() {
int no = 0;
int len;
int max_no;
int max_len = 0;
char *line, *max;
max = (char *) malloc(0);
while (mkdstr(&line, &getchar)) {
no++;
len = strlen(line);
if (len > max_len) {
max = (char *) realloc(max, len);
strcpy(max, line);
max_no = no;
max_len = len;
}
}
printf("%d %s\n", max_no, max);
return 0;
}
#define DSTR_INIT 10
int mkdstr(char **res, int (*get)(void)) {
char c, *r;
int size = DSTR_INIT;
int count = 0;
free(*res);
*res = (char *) malloc(size);
r = *res;
while ((c = (*get)()) != EOF && c != '\n') {
if (count == size) {
size *= 2;
*res = (char *) realloc(*res, size);
r = *res + count;
}
*r++ = c;
count++;
}
*r = '\0';
if (c == '\n') {
return 1;
}
else {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment