-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcla_lab_8_8_6_2-C.c
94 lines (80 loc) · 1.8 KB
/
cla_lab_8_8_6_2-C.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
char** str_split(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
count += last_comma < (a_str + strlen(a_str) - 1);
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
int main()
{
int flag = 0;
printf( "Enter a value :");
char input[100];
int mass[100];
scanf("%s", input);
char** tokens;
//printf("input=[%s]\n\n", input);
tokens = str_split(input, '.');
if (tokens)
{
int i;
for (i = 0; *(tokens + i); i++)
{
//printf("token=[%s]\n", *(tokens + i));
mass[i] = atoi(*(tokens + i));
if(i == 3)
flag = 1;
if(i == 4)
flag = 0;
free(*(tokens + i));
}
//printf("\n");
free(tokens);
}
//int size = sizeof(mass)/sizeof(int);
int i = 0;
while(i<4){
//printf("\n%d", mass[i]);
if(mass[i]>255 || mass[i] < 0)
flag = 0;
i++;
}
if(flag != 0)
printf("\n\nstring is a valid IP address");
else printf("\n\nstring is not a valid IP address");
return 0;
}