-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexercise1-24.c
104 lines (82 loc) · 2.03 KB
/
exercise1-24.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
#include <stdio.h>
#define SIZE 1000
// Variables for quotes
int double_quotes, single_quotes;
// Variables for braces and parenthesis
int brace, paren;
void in_comment(char string[]);
void in_quote(char string[]);
void in_braces(char string[]);
int main(void)
{
char string[SIZE];
int c;
int i = 0;
while ((c = getchar()) != EOF)
string[i++] = c;
string[i] = '\0';
printf("%s", string);
/* checking if any exist there */
in_comment(string);
/* make sure we have quotes either double or ordinary */
in_quote(string);
/* Checking every quote in the world */
in_braces(string);
}
void in_comment(char string[])
{
int i;
for (i = 0; string[i] != '\0'; ++i) {
if (string[i] == '/' && string[i+1] == '/')
for (i += 2; string[i++] != '\n';)
;
else if (string[i] == '/' && string[i+1] == '*') {
for (i += 2; string[i] != '*' && string[i+1] != '/'; ++i)
;
i += 2;
}
}
}
void in_quote(char string[])
{
extern int double_quotes, single_quotes;
int i;
i = double_quotes = single_quotes = 0;
while (string[i] != '\0') {
if (string[i] == '"')
++double_quotes;
else if (string[i] == '\'')
++single_quotes;
++i;
}
if (double_quotes % 2 != 0)
printf("%s\n", "Mismatched double quote");
if (single_quotes % 2 != 0)
printf("%s\n", "Mismatched single quote");
}
void in_braces(char string[])
{
extern int brace, paren;
int i;
for (i = paren = brace = 0; string[i] != '\0'; ++i) {
/* if we have found a parentheses, we increment the variable */
if (string[i] == '(')
++paren;
/* and if you have found closing parentheses, we decrement our incremented variable */
else if (string[i] == ')')
--paren;
/* the same thing here, but with braces */
else if (string[i] == '{')
++brace;
else if (string[i] == '}')
--brace;
}
while (paren > 0) {
printf("%s\n", "Mismatched parentheses");
--paren;
}
while (brace > 0) {
printf("%s\n", "Mismatched brace");
--brace;
}
}