-
Notifications
You must be signed in to change notification settings - Fork 1
/
3ques_between_num.c
90 lines (75 loc) · 1.83 KB
/
3ques_between_num.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
/*
* Take an input string parameter and determine
* if exactly 3 question marks exist between every pair of numbers that add up to 10.
* If so, return true, otherwise return false.
*
* Test cases:
*
* "arrb6???4xxbl5???eee5" => true
* "acc?7??sss?3rr1??????5" => true
* "5??aaaaaaaaaaaaaaaaaaa?5?5" => false
* "9???1???9???1???9" => true
* "aa6?9" => false
*
*/
#include <stdio.h>
#define SUM 10
#define SYMBOL '?'
#define SYMBOL_COUNT 3
#define INP_BUFLEN 50
int check_string(char string[], int sum, char symbol, int symbol_count);
int check_string(char string[], int sum, char symbol, int symbol_count)
{
int i;
int num1, num2, count, atleast_once = 0;
for (i = 0; string[i] != '\0'; i++)
{
if (string[i] >= '0' && string[i] <= '9')
{
num2 = string[i] - '0';
count = 0;
break;
}
}
for (;string[i] != '\0'; i++)
{
if (string[i] >= '0' && string[i] <= '9')
{
num1 = num2;
num2 = string[i] - '0';
if ((num1 + num2) == sum)
{
if (count != symbol_count)
{
return -1;
}
atleast_once = 1;
}
count = 0;
}
else if (string[i] == symbol)
{
count++;
}
}
if (!atleast_once)
return -1;
return 0;
}
int main()
{
char input[INP_BUFLEN];
int i = 0;
while (1)
{
fgets(input, INP_BUFLEN, stdin);
/* lazy chomp */
for (i = 0; input[i] != '\0'; i++);
input[i-1] = '\0';
if (input[0] == '\0')
break;
printf ("\r%s => %s\r\n", input,
(!check_string(input, SUM, SYMBOL, SYMBOL_COUNT))?"true":"false");
}
return 0;
}