strcmp (): string comparison function,
Function: c/s + + function, compare two strings, involving two strings corresponding to the character by comparison, until the discovery does not match, the first mismatch is small, large, if one string is the previous part of the other, it is also considered less than another string, because its null appears earlier. Set these two strings to STR1,STR2,
If STR1==STR2, returns 0;
If STR1>STR2, a positive number is returned;
If STR1<STR2, a negative number is returned.
Prototype: int strcmp (const char *str1,const char *STR2)
Return value: In principle, 0, positive and negative, but sometimes it is the difference between the ASCII values that return two characters
Note: 1) the strcmp () function compares two strings, such as arrays, string constants, etc., and cannot compare other forms of parameters such as numbers;
2) function return value is int, not char*, two string is constant form plus const;
3) Here Str1,str2 do not assert, because two of the string may be empty, but generally not so write, time to judge, otherwise there is not much significance;
My Code:
#include <stdio.h>int my_strcmp (const char* str1,const char* str2) {const char *S1 = str1;const char *s2 = str2;whi Le (*s1| | *S2)//Judge null {if (*s2 = = ' \ ' | | *s1 > *S2) {return 1;} else if (*s1 = = ' \ "| | *s1 < *S2) {return-1;} Else{return 0;} s1++;s2++;} return 0; Returns a value of 0} If all two strings are empty
Source:
the int strcmp (const char *str1,const char *STR2) {/* cannot be compared with while (*str1++==*str2++), and when it is not equal, the + + is still executed once, and the comparison value returned by return is actually the next character. The + + should be placed in the loop body. */while (*STR1 = = *str2) {if (*str1 = = ') ' Return0; str1++; str2++; } return *STR1-*str2;}
This article from "Magoyang" blog, declined reprint!
strcmp ()