-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoldLines.c
66 lines (56 loc) · 1.48 KB
/
foldLines.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#define LINE_WIDTH 20
#define MAX_WORD_LEN 28 /* less then line width */
/* Author: Scopych
Date: 21.05.2018
Purpose: the program folds long input lines
into two or more shorter lines after the last
non-blank character that occurs befor the
n-th column of input.K&R2 ex.1-22.
*/
int getWord (int head); /* extract word from input */
char word[MAX_WORD_LEN];
int ch;
int main(void) {
extern int ch;
int lindex = 0;/* line index (position) */
int lenWord = 0;/* lenght of a word */
extern char word[];/* holds current word*/
while ((ch = getchar()) != EOF) {
if (ch == ' ' || ch == '\t' || ch == '\n') {
putchar(ch);
lindex++;
} else {
lenWord = getWord(ch);
lindex += lenWord;
if (lindex < LINE_WIDTH - 2) {
printf("%s", word);
putchar(ch);
lindex++;
} else {
putchar('\n');
putchar('\0');
lindex = lenWord;
lenWord = 0;
printf("%s", word);
putchar(ch);
}
}
}
return 0;
}
int getWord (int head) {
extern char word[];
extern int ch;
int i, indx;
for (indx = 0; indx <= 100; ++indx) { /* before use a word, assign all array to 0-s.*/
word[indx] = '\0';
}
word[0] = head;
for (i = 1; i < MAX_WORD_LEN - 1
&& (ch = getchar()) != ' ' && ch != '\t'
&& ch != '\n'; ++i) {
word[i] = ch;
}
return i;
}