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.