How to Implement the C strcmp () function

Source: Internet
Author: User

The int stringcompare (char * source, char * target) function compares the source and target strings, and returns a negative integer, 0, or integer based on whether the source is smaller than, equal to, or greater than the target. The returned value is the difference between the source and target characters between the first character that is not equal to the source character and the target character.

# Include <stdio. h> int stringcompare (char * source, char * target); int main () {char str_a [] = "Welcome to www.bkjia.com "; char str_ B [] = "Welcome to www.bkjia.com"; int wait, result; result = stringcompare (str_ B, str_a); printf ("After Function Call: \ n "); printf ("result is '% d' \ n", result); scanf ("% d", & wait );} /* return negative integer, 0, or integer */int stringcompare (char * source, char * target) {int I based on the result of source which is less than, equal to, or greater than target in alphabetical order; for (I = 0; source [I] = target [I]; I ++) {if (source [I] = '\ 0') return 0; return source [I]-target [I];}

The following is implemented using pointers:

int stringcompare(char *source, char *target){for ( ; *source == *target; source++, target++)if (*source == '\0')return 0;return *source - *target;}

There is a usage for auto-increment and auto-subtraction of pointers:

/* Press val into the stack */* p ++ = val;/* bring the top element of the stack to val */val = * -- p;

These two expressions are the standard usage of both inbound and outbound stacks.

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.