-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno_uppersands_allowed.c
112 lines (88 loc) · 1.62 KB
/
no_uppersands_allowed.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* This exercise is meant to show that we can replace the &&
* and the || with something else. I figured out two ways;
* the first way is to pass it to the pre-processor and the
* second way is to replace it with if statements
*/
==============================================================
/* First way */
==============================================================
#include <stdio.h>
#define MAXLINE 100
#define AND &&
#define OR ||
int theInput(char [], int);
// This function reads a line and retunrs its length
int theInput(char s[], int upperbound)
{
int c, i, halt;
halt = 0;
for (i=0; i < upperbound-1 AND (c=getchar()) != ’\n’ AND c != EOF; i++)
{
s[i] = c;
}
if (c == '\n' OR c == '$')
{
s[i++] = c;
}
s[i] = '\0';
return i;
}
void main(void)
{
char line[MAXLINE];
while (theInput(line, MAXLINE) > 0)
{
printf("%s", line);
}
}
==============================================================
/* Second way */
==============================================================
/*
#include <stdio.h>
#define MAXLINE 100
int theInput(char [], int);*/
// This function reads a line and returns its length
/*int theInput(char s[], int upperbound)
{
int c, i, halt;
halt = 0;
for (i = 0; !halt; ++i)
{
if (i > upperbound - 1)
{
halt = 1;
}
else if ((c = getchar()) == '\n')
{
halt = 1;
}
else if (c == EOF)
{
halt = 1;
}
else
{
s[i] = c;
}
}
i--;
if (c == '\n')
{
s[i++] = c;
}
if (c == '$')
{
s[i++] = c;
}
s[i] = '\0';
return i;
}
void main(void)
{
char line[MAXLINE];
while (theInput(line, MAXLINE) > 0)
{
printf("%s", line);
}
}*/