-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex13-3.c
57 lines (48 loc) · 1.39 KB
/
ex13-3.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
#include <stdio.h>
/////////////////////////////////////////////////////////
// Exercise 13 - Extra Credit #3
// Convert the switch statement to an if statement
//////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
if(argc == 1) {
printf("ERROR: please enter one or more argument.\n");
return 1;
}
int i;
for(i = 1; i < argc; i++) {
char *param = argv[i];
int j;
for(j = 0; param[j] != '\0'; j++) {
char letter = param[j];
char orig_letter = letter;
if(letter >= 'A' && letter <= 'Z') {
// PROTIP: man ascii
letter += 32;
}
// I like the if/else approach a little better as it is more
// compact, but I suspect that switch could be reformatted to be
// just as compact.
if (letter == 'a') {
printf("%d: '%c'\n", j, orig_letter);
} else if (letter == 'e') {
printf("%d: '%c'\n", j, orig_letter);
} else if (letter == 'i') {
printf("%d: '%c'\n", j, orig_letter);
} else if (letter == 'o') {
printf("%d: '%c'\n", j, orig_letter);
} else if (letter == 'u') {
printf("%d: '%c'\n", j, orig_letter);
} else if (letter == 'y') {
if(i > 2) {
// it's only sometimes Y
printf("%d: '%c'\n", j, orig_letter);
}
} else {
printf("%d: '%c' is not a vowel\n", j, orig_letter);
}
}
if(i + 1 != argc) { printf("\n"); } // don't print on last parameter
}
return 0;
}