Загрузка данных
#include <stdio.h>
#include <stdlib.h>
#define MAX_TEXT 101
#define MAX_LINE 101
int check_key(int argc, char *argv[]);
int read_data(int *width, char *text);
int str_len(char *str);
void remove_newline(char *text);
void skip_spaces(char *text, int *pos);
int word_len(char *text, int pos);
void flush_line(char *line, int *line_len, int *first_line);
void add_char(char *line, int *line_len, char c);
void add_word(char *line, int *line_len, char *text, int *pos, int len);
void split_word(char *line, int *line_len, int *first_line, char *text, int *pos, int width);
void process_word(char *line, int *line_len, int *first_line, char *text, int *pos, int width);
void print_text(char *text, int width);
int main(int argc, char *argv[]) {
int width;
char text[MAX_TEXT];
if (!check_key(argc, argv) || !read_data(&width, text)) {
printf("n/a");
} else {
print_text(text, width);
}
return 0;
}
int check_key(int argc, char *argv[]) {
return argc == 2 && argv[1][0] == '-' && argv[1][1] == 'w' && argv[1][2] == '\0';
}
int read_data(int *width, char *text) {
int result = 1;
if (scanf("%d\n", width) != 1 || *width <= 1 || fgets(text, MAX_TEXT, stdin) == NULL) {
result = 0;
} else {
remove_newline(text);
}
return result;
}
void print_text(char *text, int width) {
int pos = 0;
int line_len = 0;
int first_line = 1;
char line[MAX_LINE];
skip_spaces(text, &pos);
while (text[pos] != '\0') {
process_word(line, &line_len, &first_line, text, &pos, width);
skip_spaces(text, &pos);
}
flush_line(line, &line_len, &first_line);
}
void process_word(char *line, int *line_len, int *first_line, char *text, int *pos, int width) {
int len = word_len(text, *pos);
if (*line_len == 0) {
if (len <= width) {
add_word(line, line_len, text, pos, len);
} else {
split_word(line, line_len, first_line, text, pos, width);
}
} else if (*line_len + 1 + len <= width) {
add_char(line, line_len, ' ');
add_word(line, line_len, text, pos, len);
} else if (len > width) {
split_word(line, line_len, first_line, text, pos, width);
} else {
flush_line(line, line_len, first_line);
}
}
void split_word(char *line, int *line_len, int *first_line, char *text, int *pos, int width) {
if (*line_len > 0 && width - *line_len - 2 > 0) {
int count = width - *line_len - 2;
add_char(line, line_len, ' ');
add_word(line, line_len, text, pos, count);
add_char(line, line_len, '-');
flush_line(line, line_len, first_line);
} else if (*line_len > 0) {
flush_line(line, line_len, first_line);
}
while (word_len(text, *pos) > width) {
add_word(line, line_len, text, pos, width - 1);
add_char(line, line_len, '-');
flush_line(line, line_len, first_line);
}
add_word(line, line_len, text, pos, word_len(text, *pos));
}
void flush_line(char *line, int *line_len, int *first_line) {
if (*line_len > 0) {
if (!*first_line) {
putchar('\n');
}
for (int i = 0; i < *line_len; i++) {
putchar(line[i]);
}
*line_len = 0;
*first_line = 0;
}
}
void add_char(char *line, int *line_len, char c) {
line[*line_len] = c;
(*line_len)++;
}
void add_word(char *line, int *line_len, char *text, int *pos, int len) {
for (int i = 0; i < len; i++) {
add_char(line, line_len, text[*pos]);
(*pos)++;
}
}
void remove_newline(char *text) {
int len = str_len(text);
if (len > 0 && text[len - 1] == '\n') {
text[len - 1] = '\0';
}
}
int str_len(char *str) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
void skip_spaces(char *text, int *pos) {
while (text[*pos] == ' ') {
(*pos)++;
}
}
int word_len(char *text, int pos) {
int len = 0;
while (text[pos + len] != '\0' && text[pos + len] != ' ') {
len++;
}
return len;
}