Turn: implementation and analysis of strcmp Functions

Source: Internet
Author: User

Conversion from: Implementation and explanation of strcmp Functions

The strcmp function is a basic function in C/C ++. It compares two strings and returns the comparison result. The function form is as follows:
Int strcmp (constchar * str1, constchar * str2 );
Str1 and str2 can be string constants or string variables, and the return value is an integer. The returned results are as follows:
① If str1 is less than str2, return a negative value or-1 (VC returns-1); ② If str1 is equal to str2, return 0;
③ If str1 is greater than str2, return a positive value or 1 (VC returns 1 );
The strcmp function is actually used to compare the ASCII code of a character. The implementation principle is as follows: first, compare the first character of the two strings. If the two strings are not equal, then the comparison is stopped and the results of comparing the two ASCII codes are obtained. If the two ASCII codes are equal, the second character and the third character are compared. Regardless of the two strings, The strcmp function can obtain the result by comparing a string with the terminator '/0' at most. There can be many strcmp algorithms, But I think so many algorithms can be divided into two types: one is to use the subtraction operation to determine the result, and the other is to use the comparison operation (=) result. The code for Subtraction is as follows:

 1 int strcmp(const char *str1, const char *str2) 2 { 3     int ret=0; 4     while( !(ret = *(unsigned char*)str1 - *(unsigned char*)str2 ) && *str1 ) 5     { 6         str1++; 7         str2++; 8     } 9     if(ret < 0)10         return -1;11     else if(ret > 0) 12         return 1;13     return 0;    14 }

Pay attention to the following points for this function:
① Use * (unsignedchar *) str1 instead of * str1. This is because the input parameter is a signed number, and the value range of the signed character is-128 ~ 127, the value range of the unsigned character is 0 ~ 255, while the ASCII value of the string does not have a negative value. If it is not converted to an unsigned number, this error occurs during subtraction. For example, the value of str1 is 1, and the value of str2 is 255.
Ret =-254 when calculated as an unsigned number, the result is a negative value, ret = 2 when calculated correctly as a signed number, the result is positive, error
② In the while loop, ret = * (unsignedchar *) str1-* (unsignedchar *) str2) & * str1. Finally, you can replace str2 with str1, because the subtraction has been done before, no matter which one is first '\ 0', it will exit. Because the final match with str1 is to determine whether str1 ends, that is, whether it is '\ 0 '.
③ This function does not determine if the parameter is null, so the program will crash when null is passed in. If someone else says that commercial code will determine whether it is null before calling strcmp, I don't need to judge null. I tested string on vc6. the strcmp (null, null) in H also crashes. This can be determined based on the actual situation.
To determine whether null changes the code using the following method, you can add assert (null! = Str1) & (null! = Str2 ))
However, it should be noted that assert is a macro that only works in the debug version and is a harmless test performed during the debug version. You can also
To judge null, we must use other code to judge. You can add If judgment before the program.
If (null! = Str1) & (null! = Str2 )){
Return0 ;}
I use cfree5 to test strcmp (null, null) in sting. h, and the return value of the program is 0 (strcmp (null, str1) Crash). Here we can return other values such as-2. We can also add while before the function to judge while (null! = Str1) & (null! = Str2 )){
// Strcmp implementation code}
Return0;

You can use while to judge each character. The comparison algorithm (=) is as follows:

 1 int strcmp( const char *str1, const char *str2 ) 2 { 3     while( (*str1) && ( *str1 == *str2 ) ) 4     { 5         str1++; 6         str2++; 7     } 8     if( *(unsigned char*)str1 > *(unsigned char*)str2 ) 9         return 1;10     else if( *(unsigned char*)str1 < *(unsigned char*)str2 )11         return -1;12     else13         return 0;14 }

 

There is also a for implementation and an incorrect implementation. The table is no longer displayed here.

 

Turn: implementation and analysis of strcmp Functions

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.