#include #include int main() { char *data = NULL; int data_size = 0; int file_size = 0; int *offsets = NULL; int offset_size = 0; int line_count = 0; int newline = 1; int max_width = 0; int width; int c, r; while( (c = getchar()) != EOF ) { /* Record offset for start of current line */ if( newline ) { if( line_count + 1 >= offset_size ) { offset_size = offset_size ? offset_size * 2 : 1024; offsets = (int*)realloc(offsets, sizeof(int) * offset_size); if( offsets == NULL ) return puts("Out of memory"); } offsets[line_count++] = file_size; } /* Buffer input character */ if( c != '\n' ) { if( file_size >= data_size ) { data_size = data_size ? data_size * 2 : 1024; data = (char*)realloc(data, data_size); if( data == NULL ) return puts("Out of memory"); } data[file_size++] = c; newline = 0; width = file_size - offsets[line_count - 1]; if( width > max_width ) max_width = width; } else { newline = 1; } } offsets[line_count] = file_size; /* Output columns */ for(c = 0; c < max_width; c++) { for(r = line_count; r-- > 0;) { width = offsets[r + 1] - offsets[r]; if( c >= width ) { putchar(' '); } else { putchar(data[offsets[r] + c]); } } putchar('\n'); } return 0; }