-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp093.c
47 lines (44 loc) · 1.15 KB
/
p093.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
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int len;
char **result;
void f(const int x, const int y, const char *s, int *returnSize)
{
if (x == 4)
{
(*returnSize)++;
int i;
for (i = 0; i < len+3; i++)
result[*returnSize][i] = result[(*returnSize)-1][i];
return;
}
int val = 0;
int i;
for (i = y; i <= y+2 && i < len; i++)
{
val = (val<<3)+(val<<1)+s[i]-'0';
if (val > 255) break;
result[*returnSize][x+i] = s[i];
if (len-i <= 10-x-x-x && len-i >= 4-x)
{
if (x < 3) result[*returnSize][x+i+1] = '.';
f(x+1, i+1, s, returnSize);
}
if (s[y] == '0') break;
}
}
char** restoreIpAddresses(char* s, int* returnSize) {
len = strlen(s);
result = (char**)malloc(sizeof(char*)*81);
int i, j;
for (i = 0; i < 81; i++)
{
result[i] = (char*)malloc(sizeof(char)*(len+4));
result[i][len+3] = '\0';
}
(*returnSize) = 0;
f(0, 0, s, returnSize);
return result;
}