Talk C Chestnut Bar (66th: C language Instance--diy string comparison function)

Source: Internet
Author: User
Tags strcmp

Hello, everyone, crossing. We're talking about the DIY string connection function example, this time we say the example is:DIY string comparison function . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

We introduced the string comparison function in the previous Zhanghuizhong, but some crossing have forgotten, in order to deepen crossing's impression of the string comparison function, we are ready to DIY string comparison function. Just do it by yourself!

We introduced a total of two string comparison functions in the previous chapter:strcmp,strncmp. Next we describe how to DIY these two string comparison functions.

DIY strcmp function

int diy_strcmp (const char s1, const char s2)

    • 1. In the string pointed to by the S1 and S2, each character is taken to compare;
    • 2. Determine the ASCII value size of these two characters, if their values are equal, then return to the first step, compare the next character; otherwise, go to the next step;
    • 3. Returns the ASCII code difference between the two characters;
    • 4. Repeat steps 1 through 3 until the character is removed by S1 or S2 's small tail.

Here is the code I wrote, please refer to:

intDIY_STRCMP (const char*s1, const char * s2) {intres =0;if(NULL = = S1 | | NULL = = s2) {printf("NULL pointer error \ n");return 0; } while(*s1!=' + '&&*s2!=' + ')    {if(*s1==*s2) {s1++;        s2++; }Else{res =*s1-*s2; Break; }    }if(res = =0) res =*s1-*s2;//The ASCII value of \0Is0    returnRes;}

As you can see from the code above, we use the ASCII code difference of two characters as the result of comparison, and we calculate the result in the comparison process. If the result is not computed, it is possible to encounter a small tail of S1 or S2, then we will calculate it again. When calculating, the ASCII code value of the null character is skillfully exploited. If you encounter null characters during the comparison, stop the comparison. At this time, at least one of the two characters is a null character when the result is calculated. The null character has a value of zero, minus the other characters, and you get a negative number. Subtract it with a different character, and you get a positive number. And that's exactly what we want.

Here is the code for the STRCMP function in the standard library, please compare with our DIY code:

/** * strcmp-compare, Strings * @cs: one string * @ct: Another string */#undef strcmpintstrcmpConst Char*cs,Const Char*CT) {unsignedCharC1, C2; while(1) {C1 = *cs++; C2 = *ct++;if(C1! = C2)returnC1 < C2? -1:1;if(!C1) Break; }return 0;}

By contrast, you can see that the code provided by the standard library is more concise, but the comparison is the same. In the returned results, the code in the standard library returns only the three values of -1,1 and 0. And the return value in our DIY code is related to the character in the string, and of course, two strings are still returned 1 when they are equal.

DIY strncmp function

int diy_strncmp (const char s1, const char s2,int N)

    • 1. In the string pointed to by the S1 and S2, each character is taken to compare;
    • 2. Determine the ASCII value size of these two characters, if their values are equal, then return to the first step, compare the next character; otherwise, go to the next step;
    • 3. Returns the ASCII code difference between the two characters;
    • 4. Repeat steps 1 through 3 until any one of the following two conditions is met.
      • Condition One: The character taken out is a small tail of S1 or S2;
      • Condition Two: The n characters have been compared;

Here is the code I wrote, please refer to:

intDIY_STRNCMP (const char*s1, const char * s2,intN) {intres =0;if(NULL = = S1 | | NULL = = s2) {printf("NULL pointer error \ n");return 0; } while(n) {if(*s1!=' + '&&*s2!=' + ')        {if(*s1==*s2) {s1++;                s2++;            n--; }Else{res =*s1-*s2; Break; }        }Else             Break; }if(N >0)//diffenent Conditions res =*s1-*s2;//The ASCII value of \0Is0    returnRes;}

Through the above code, you can see strncmp in the comparison process to increase the judgment of N, if n is less than S1 or S2 in the number of characters, the comparison process and strcmp the same, otherwise you need to calculate the comparison results, that is, the code "if (n>0)" this case.

Below is the code of STRNCMP in standard library, please compare with our DIY code:

/** * Strncmp-compare-length-limited strings * @cs: one string * @ct: Another string * @co UNT: The maximum number of bytes to compare */intstrncmpConst Char*cs,Const Char*CT, size_t count) {unsignedCharC1, C2; while(count)                {C1 = *cs++; C2 = *ct++;if(C1! = C2)returnC1 < C2? -1:1;if(!C1) Break;        count--; }return 0;}

By contrast, you can see that the code provided by the standard library is more concise, but the comparison is the same. In the returned results, the code in the standard library returns only the three values of -1,1 and 0. And the return value in our DIY code is related to the character in the string, and of course, two strings are still returned 1 when they are equal.

Crossing, I've compiled these two DIY functions into a file and added detailed comments, and I've used the test case in the previous chapter to test it. The text does not write code, the detailed code put in my resources, you can click here to download the use. The previous chapter back in the program can click here to download.

Here is the result of the program running, please compare with the program running results in the previous chapter.

----------- testing diy_strcmp ----------- abcdabcdABCDabcdabcd = abcd  ----------- testing diy_strncmp ----------- 3of2of2ofABCD5of

You crossing, for example of DIY string comparison function Let's talk about this. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (66th: C language Instance--diy string comparison function)

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.