forked from jhpy1024/CProgrammingLanguageExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_7-2.c
58 lines (52 loc) · 1.31 KB
/
exercise_7-2.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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
enum
{
MAX_LINE_LENGTH = 80,
START_NON_GRAPHIC = 0,
END_NON_GRAPHIC = 31,
TAB = '\t',
NEWLINE = '\n',
CARRIAGE_RETURN = 13
};
/*
* Prints non-graphic characters in hexadecimal.
*
* Characters with ASCII codes 0 to 31 (excluding 9 (tab), 10 (newline), and 13 (carriage return)) are
* considered to be non-graphic characters.
*
* Breaks a line when its length >= MAX_LINE_LENGTH.
*/
int main()
{
unsigned line_length = 0;
int current_char = EOF;
while ((current_char = getchar()) != EOF)
{
if (line_length >= MAX_LINE_LENGTH)
{
putchar('\n');
line_length = 0;
/* Don't print any whitespace at the start of the newline. */
if (!isspace(current_char))
{
putchar(current_char);
}
}
else
{
if (current_char >= START_NON_GRAPHIC && current_char <= END_NON_GRAPHIC
&& current_char != TAB && current_char != NEWLINE && current_char != CARRIAGE_RETURN)
{
printf("0x%X", current_char);
}
else
{
putchar(current_char);
}
++line_length;
}
}
return EXIT_SUCCESS;
}