比较内存区域buf1和buf2的前count个字节但不区分字母的大小写及手动使用指针法实现 ------ _memicmp

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<memory.h>
 5 
 6 // _memicmp  判定这片内存内容相同还是不同 
 7 
 8 int  pointmemicmp(const void * _Buf1, const void * _Buf2, size_t _Size);// 指针法手动实现 _memicmp
 9 void main()
10 {
11     int a[5] = { 1, 2, 6, 4, 5 };
12     int b[5] = { 1, 2, 3, 5, 4 };
13     char str1[128] = "zhangsambeijing";
14     char str2[128] = "zhangsanshanghai";
15     int i = _memicmp(a, b, 12);// 12 代表是字节  相同返回0 不相同返回1 
16     int j = _memicmp(str1,str2,8);// 相同返回0 不相同,如果str1的其中一个字符大于str2(b>n) 返回-1  反之返回1
17     printf("i = %d , j = %d\n",i,j);
18 
19     /**************调用手动实现的方法实现判断*************************/
20     int m = pointmemicmp(a, b, 12);
21     int n = pointmemicmp(str1, str2, 8); 
22     printf("m = %d ,n = %d\n",m,n);
23     system("pause");
24 }
25 
26 // 指针法手动实现 _memicmp
27 int  pointmemicmp(const void * _Buf1, const void * _Buf2, size_t _Size)
28 {
29     if (_Buf1 == NULL || _Buf2 == NULL)
30     {
31         return 0;
32     }
33     if (_Size == 0)
34     {
35         return 0;
36     }
37     char *str1 = _Buf1;
38     char *str2 = _Buf2;//起始点
39 
40     //char * last = str1 + _Size;//终点
41     int i = 0;
42     while ((*str1 == *str2) && i<_Size)// 相等一直循环下去,没有到终点
43     {
44         str1++;
45         str2++;
46         i++;
47     }
48 
49     if (i == _Size)
50     {
51         return 0;
52     }
53     else
54     {
55         if (*str1>*str2)
56         {
57             return 1;
58         }
59         else
60         {
61             return -1;
62         }
63     }
64 }

 

posted on 2015-05-12 16:25  Dragon-wuxl  阅读(487)  评论(0)    收藏  举报

导航