"Face Test" C language: Simulation to achieve memcmp, try to compare the difference between memcmp and strcmp,strncmp

Source: Internet
Author: User
Tags strcmp

The simulation implements the memory comparison function memcmp:


The function is similar to strcmp, and can be used for the same string comparison, and returns a value of 0 if the same. If the former is greater than the latter, an integer value greater than 0 is returned, or an integer value less than 0 is returned.


The difference is:


STRCMP can only compare strings, memcmp is a memory comparison function, in principle it is compared to memory, but in fact not all can be compared when actually implemented, for example, float, but we can at least compare strings and int type. For strcmp,strncmp comparison: str1, str2 is the two string to compare, n is the number of characters to compare, and the function strcmp () does not, strcmp () can compare all the strings (because it is looking for the end of the string "\").


For the implementation of strcmp, you can view my blog http://10740184.blog.51cto.com/10730184/1714512


For the implementation of STRNCMP, you can view my blog http://10740184.blog.51cto.com/10730184/1715207

The code is as follows:


#define  _CRT_SECURE_NO_WARNINGS 1#include<stdio.h> #include <stdlib.h> #include <assert.h &GT;INT&NBSP;MY_MEMCMP (Const void *p1, const void *p2, size_t count) {     assert (p1);     assert (p2);     char *dest =   (char *) p1;    char *src =  (char *) p2;     while  (COUNT&NBSP;&AMP;&AMP;&NBSP;*DEST&NBSP;==&NBSP;*SRC)     {         count--;        dest++;         src++;    }    if  ( count == 0)         return 0;     else        return *dest  - *src ;} Int main () {    /*int arr1[] = { 1, 3, 20, 5 };             //Comparison integral type     int arr2[] = { 1, 3 , 10, 7 };*/    char arr1[] =  "Hello";                     //Comparing strings     char arr2[] =  "Hello world";     int len  = sizeof (arr1)  / sizeof (arr1[0]);     int ret = my_ MEMCMP (arr1, arr2, 12);     printf ("%d",  ret);     system ("pause");     return 0;}


"Face Test" C language: Simulation to achieve memcmp, try to compare the difference between memcmp and strcmp,strncmp

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.