-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex14-extra.c
75 lines (63 loc) · 1.93 KB
/
ex14-extra.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
#include <stdio.h>
#include <ctype.h>
#include <string.h> // Extra credit #2
#include <locale.h> // Extra credit #3
//////////////////////////////////////////////////////////////
// EXTRA CREDIT - Exercise 14
// 1) Inline can_print_it
// 2) Have print_arguments figure out how long each argument
// is using the strlen function, and then pass that to
// print_letters. Then re-write print_letters so it only
// processes this fixed length and doesn't look for '\0'
// 3) Read up on isalpha and isblank with man, then use
// similar functions to only print out digits or other
// categories of characters.
// 4) Read about "K&R syntax"
//////////////////////////////////////////////////////////////
// forward declarations
void print_chars(char arg[], int len);
void print_arguments(int argc, char *argv[])
{
int i = 0;
for(i = 0; i < argc; i++) {
// Extra Credit #2 - use strlen instead of looking for \0
char * word = argv[i];
int len = strlen(word);
print_chars(word, len);
}
}
void print_chars(char arg[], int len)
{
int i = 0;
for(i = 0; i < len; i++) {
char ch = arg[i];
// Ugly use of conditional compilation instead of refactoring or
// command-line parsing because I wanted to learn how it works.
// NOTE: http://stackoverflow.com/q/2335888/7507
#if use_isdigit == 1
// Extra Credit #3 - use other character functions
if(isdigit(ch)) {
#elif use_isalnum == 1
if(isalnum(ch)) {
#elif use_not_isalnum == 1
if(!isalnum(ch)) {
#else
// Extra Credit #1 - inline some functions
if(isalpha(ch) || isblank(ch)) {
#endif
printf("'%c' == %d\n", ch, ch);
}
}
printf("\n");
}
int main(int argc, char *argv[])
{
// Extra Credit #3
// Much of the behavior of the various character functions
// is dependant on the Locale, so I'm testing out the
// setlocale() function:
char* locale_name = setlocale(LC_NAME, "");
printf("The locale name is %s\n", locale_name);
print_arguments(argc, argv);
return 0;
}