File size: 1,025 Bytes
bff4ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "../../string.h"

int strcmp(char *s1, char *s2)
/*@ With str1 str2
    Require valid_string(str1) && valid_string(str2) &&
            string_length(str1) < INT_MAX && string_length(str2) < INT_MAX &&
            store_string(s1, str1) * store_string(s2, str2)
    Ensure strcmp_result(str1, str2, __return) &&
           store_string(s1, str1) * store_string(s2, str2)
*/
{
  int i = 0;

  while (s1[i] != 0 && s1[i] == s2[i]) {
    i++;
  }
  return s1[i] - s2[i];
}

int strncmp(char *s1, char *s2, int n)
/*@ With str1 str2
    Require valid_string(str1) && valid_string(str2) &&
            0 <= n && n < INT_MAX &&
            string_length(str1) < INT_MAX && string_length(str2) < INT_MAX &&
            store_string(s1, str1) * store_string(s2, str2)
    Ensure strncmp_result(str1, str2, n, __return) &&
           store_string(s1, str1) * store_string(s2, str2)
*/
{
  int i = 0;

  while (i < n && s1[i] != 0 && s1[i] == s2[i]) {
    i++;
  }
  if (i == n) {
    return 0;
  }
  return s1[i] - s2[i];
}