-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaysToDecode.c
49 lines (41 loc) · 969 Bytes
/
WaysToDecode.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
int WaysToDecode(char *message)
{
if (strlen(message) == 0)
{
// We've processed entire message. Successful decode!
return 1;
}
else
{
int num_ways = 0;
for (int i=1; i<=26; i++)
{
bool match = true;
char temp[3];
sprintf(temp, "%d", i);
for (int j=0; j<strlen(temp); j++)
{
if (temp[j] != message[j])
{
match = false;
break;
}
}
if (match)
{
num_ways += WaysToDecode(&message[strlen(temp)]);
}
}
return num_ways;
}
}
int main()
{
char *message = "123456781223123212";
printf("There are %u ways to decode the message\n",WaysToDecode(message));
return 0;
}