strcmp ()

Source: Internet
Author: User

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 ()

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.