-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome.c
53 lines (48 loc) · 1.32 KB
/
palindrome.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
/*@ requires \valid(a+(0..size-1));
assigns \nothing;
ensures \result == 0 || \result == 1;
behavior nil:
assumes size == 0;
ensures \result == 1;
behavior palindrome:
assumes size > 0;
assumes \forall integer i; 0 <= i < size ==> a[i] == a[size - i - 1];
ensures \result == 1;
behavior non_palindrome:
assumes size > 0;
assumes \exists integer i; 0 <= i < size && a[i] != a[size - i - 1];
ensures \result == 0;
complete behaviors;
disjoint behaviors;
*/
int palindrome(int a[], unsigned size)
{
int res = 1;
unsigned i;
/*@ loop invariant 0 <= i <= size / 2;
loop invariant (\forall integer j; 0 <= j < i ==> a[j] == a[size - j - 1]) <==> (res == 1);
//loop invariant (a[i] != a[size - i - 1]) <==> (res == 0);
loop invariant res == 0 || res == 1;
loop assigns res;
loop variant size - i;
*/
for(i = 0; i < size / 2; ++i) {
if (a[i] != a[size - i - 1]) {
res = 0;
break;
}
}
return res;
}
#ifdef OUT_OF_TASK
#include <stdio.h>
int main(void)
{
int a[] = {1,2,3,4,5,6,7,8,9,10};
int b[] = {1,2,3,4,5,5,4,3,2,1};
int size = sizeof(a) / sizeof(a[0]);
printf("res: %d\n", palindrome(a, size));
printf("res: %d\n", palindrome(b, size));
return 0;
}
#endif