Skip to content

Commit d0e4da4

Browse files
committed
revised code
1 parent 7dfe28a commit d0e4da4

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

chapter07/7-2.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,40 @@
55
*
66
* By Faisal Saadatmand
77
*/
8-
#include <stdio.h>
8+
99
#include <ctype.h>
10+
#include <stdio.h>
1011

11-
#define MAXLENGTH 81
12+
#define MAXLEN 80
13+
#define NCHAR 6 /* number of actually printed characters */
14+
15+
/* inclen: increment line's length by the number of actually printed
16+
* characters. Break the line if its too long. */
17+
int inclen(int len, int n)
18+
{
19+
if ((len += n) < MAXLEN)
20+
return len;
21+
printf("\n");
22+
return n;
23+
}
1224

1325
int main(void)
1426
{
15-
char c;
16-
int len; /* length of currently read line */
27+
int c;
28+
int len; /* length of currently read line */
1729

18-
for (len = 0; (c = getchar()) != EOF; len++) {
19-
if (len == MAXLENGTH) { /* simple line breaking */
20-
printf("\n");
21-
len = 0;
22-
}
23-
if (!isgraph(c)) /* print in hexadecimal */
24-
printf("%x", c);
25-
else
30+
len = 0;
31+
while ((c = getchar()) != EOF)
32+
if (!isgraph(c)) {
33+
printf(" 0x%02o ", c); /* replace by NCHAR hex characters */
34+
len = inclen(len, NCHAR);
35+
if (c == '\n') {
36+
printf("\n"); /* break the line */
37+
len = 0;
38+
}
39+
} else { /* graphical character */
2640
printf("%c", c);
27-
}
28-
printf("\n");
41+
len = inclen(len, 1);
42+
}
2943
return 0;
3044
}

0 commit comments

Comments
 (0)