-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkey_typing.c
71 lines (63 loc) · 1.45 KB
/
monkey_typing.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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
clock_t start_time = clock();
int count = typing();
printf ("GOT MATCHED! When %d words generated.\n", count);
printf ("%f\n", (float)(clock() - start_time) / CLOCKS_PER_SEC);
}
int typing()
{
char *all_chars = "abcdefghijklmnopqrstuvwxyz";
char *match = "match";
int match_length = getlen(match);
srand((unsigned)clock());
int i;
for (i = 0; ; i++)
{
int j;
char word[match_length];
for (j=0; j < match_length; j++)
{
int rand_int = rand() % getlen(all_chars);
word[j]=all_chars[rand_int];
}
//print_str(word);
if (strcmp(word, match))
{
return i;
}
}
}
int getlen(char *a){
int i=0;
while(a[i]!='\0'){
i++;
}
return i;
}
int print_str(char *a)
{
int i = 0;
while (a[i] != '\0')
{
printf("%c", a[i]);
i++;
}
printf ("\n");
return 0;
}
int strcmp(char *a, char *b)
{
int i;
if (getlen(a) != getlen(b))
return 0;
for (i=0; i < getlen(a); i++)
{
if (a[i] != b[i])
return 0;
}
return 1;
}