Skip to content

Commit 637c0ee

Browse files
committed
7.1 standard input output
1 parent 5626303 commit 637c0ee

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <ctype.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
#define LINE 80
6+
7+
int main(void)
8+
{
9+
int c, col = 0;
10+
while ((c = getchar()) != EOF)
11+
{
12+
if (isprint(c))
13+
{
14+
putchar(c);
15+
col++;
16+
}
17+
else if (c == '\n')
18+
{
19+
printf("\\n");
20+
col += 2;
21+
}
22+
else if (c == '\t')
23+
{
24+
printf("\\t");
25+
col += 2;
26+
}
27+
else if (c == '\b')
28+
{
29+
printf("\\b");
30+
col += 2;
31+
}
32+
else
33+
{
34+
printf("\\x%02X", c); // hex representation
35+
col += 4;
36+
}
37+
38+
if (col >= LINE)
39+
{
40+
putchar('\n');
41+
col = 0;
42+
}
43+
}
44+
return EXIT_SUCCESS;
45+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <ctype.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
int main(int argc, char* argv[])
7+
{
8+
if (!argc)
9+
return EXIT_FAILURE;
10+
11+
int (*convert)(int) = NULL;
12+
if (strstr(argv[0], "upper"))
13+
{
14+
convert = toupper;
15+
}
16+
else if (strstr(argv[0], "lower"))
17+
{
18+
convert = tolower;
19+
}
20+
21+
int c;
22+
23+
while ((c = getchar()) != EOF)
24+
{
25+
putchar(convert(c));
26+
}
27+
return EXIT_SUCCESS;
28+
}

0 commit comments

Comments
 (0)