Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define print_space printf(" ")
- enum { maxlen = 1000000 };
- int K;
- char s[maxlen+1];
- int spaces, ln;
- void clenup(void)
- {
- s[0] = 0;
- spaces = ln = 0;
- }
- void format_string(void)
- {
- if(spaces == 0)
- {
- printf("%s", s);
- int i;
- for(i = ln; i < K; i++)
- print_space;
- printf("\n");
- clenup();
- return;
- }
- int space_size = (K-ln) / spaces,
- extra_spaces = (K-ln) % spaces;
- char *c;
- for(c = s; *c; c++)
- {
- putchar(*c);
- if(*c == ' ')
- {
- int i;
- for(i = 0; i < space_size; i++)
- print_space;
- if(extra_spaces)
- {
- print_space;
- extra_spaces--;
- }
- }
- }
- printf("\n");
- clenup();
- }
- int main(void)
- {
- scanf("%d", &K);
- clenup();
- char new_word[K];
- while(scanf("%s", new_word) != EOF)
- {
- if(ln)
- if(ln + 1 + strlen(new_word) > K)
- format_string();
- if(ln)
- {
- strcat(s, " ");
- spaces++;
- ln++;
- }
- strcat(s, new_word);
- ln += strlen(new_word);
- }
- if(ln)
- format_string();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement