-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypad.c
78 lines (67 loc) · 1.62 KB
/
keypad.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
#include <stdio.h>
#include <string.h>
int calculate_typing_time(char *message)
{
char *keypad[] = {
"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int length = strlen(message);
int total_time = 0;
int prev_key = -1;
int prev_count = 0;
for (int i = 0; i < length; i++)
{
char c = message[i];
int key, count;
if (c >= '2' && c <= '9')
{
key = c - '0';
count = strchr(keypad[key], c) - keypad[key] + 1;
}
else if (c == '0')
{
key = 0;
count = 1;
}
else if (c == '1')
{
key = 1;
count = 1;
}
else if (c == ' ')
{
key = 0;
count = 2;
}
else if (c == '.')
{
key = 1;
count = 2;
}
if (prev_key == key)
{
total_time += 0.7;
total_time += count * 0.2;
}
else
{
if (prev_key != -1)
total_time += 0.4;
total_time += count * 0.2;
}
prev_key = key;
prev_count = count;
}
return total_time * 1000;
}
int main()
{
char message[100];
printf("Enter the message: ");
fgets(message, sizeof(message), stdin);
// Remove trailing newline character if present
if (message[strlen(message) - 1] == '\n')
message[strlen(message) - 1] = '\0';
int typing_time = calculate_typing_time(message);
printf("Minimum time to type the message: %d milliseconds\n", typing_time);
return 0;
}