-
Notifications
You must be signed in to change notification settings - Fork 2
/
gess.c
94 lines (94 loc) · 1.21 KB
/
gess.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
#include <stdio.h>
#include <string.h>
int i=0,j=0;
void num(char a[])
{
srand(time(NULL));
for (i=0;i<4;i++)
{
a[i]=rand()%10 + '0';
for(j=0;j<i;j++)
if(a[j]==a[i])
{
i--;
break;
}
}
return ;
}
void enter(char b[],int c)
{
int l=0;
x: printf("Please input four different letters:\n");
fgets(b,c,stdin);
l=strlen(b)-1;
if(l!=4)
{
printf("Invalid input\n");
goto x;
}
else
{
for(i=0;i<4;i++)
{
if(b[i]>='0'&&b[i]<='9')
{
for(j=0;j<i;j++)
if(b[j]==b[i])
{
printf("Invalid input\n");
goto x;
}
}
else
{
printf("Invalid input\n");
goto x;
}
}
}
return ;
}
int compare(char a[],char b[])
{
int n=0,m=0;
for (i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(a[i]==b[j]&&i==j)
n++;
else if(a[i]==b[j]&&i!=j)
m++;
}
if(n+m==0)
printf("0000");
else
{
for(i=0;i<n;i++)
printf("A");
for(i=0;i<m;i++)
printf("B");
}
printf ("\n");
return n;
}
int main (void)
{
int n=0,m=0;
char a[4];
char b[20];
num(a);
while (n!=4)
{
enter(b,20);
n=compare(a,b);
m++;
if(m==15)
{
printf("YOU FAILT\n");
return 0;
}
}
printf ("YOU BET!\n");
return 0;
}