-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise16.c
84 lines (71 loc) · 1.34 KB
/
exercise16.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// C Primer Plus
// Chapter 11 Exercise 16:
// Write a program that reads input until end-of-file and echoes it to the
// display. Have the program recognize and implement the following command-line
// arguments:
// - p Print input as is
// - u Map input to all uppercase
// - l Map input to all lowercase
// Also, if there are no command-line arguments, let the program behave as if
// the –p argument had been used.
#include <stdio.h>
#include <ctype.h>
void map_identity(void);
void map_uppercase(void);
void map_lowercase(void);
int main(int argc, char *argv[])
{
if (argc == 1)
{
map_identity();
return 0;
}
else if (argc > 2 || (argc == 2 && argv[1][0] != '-'))
{
printf("Usage: program_name [-p | -l | -u]\n");
return 1;
}
else
switch(argv[1][1])
{
case ('p'):
map_identity();
break;
case ('u'):
map_uppercase();
break;
case ('l'):
map_lowercase();
break;
default:
printf("Usage: program_name [-p | -l | -u]\n");
return 1;
}
return 0;
}
void map_identity(void)
{
char ch;
while((ch = getchar()) != EOF)
putchar(ch);
}
void map_uppercase(void)
{
char ch;
while((ch = getchar()) != EOF)
{
if (islower(ch))
ch = toupper(ch);
putchar(ch);
}
}
void map_lowercase(void)
{
char ch;
while((ch = getchar()) != EOF)
{
if (isupper(ch))
ch = tolower(ch);
putchar(ch);
}
}