Current location - Loan Platform Complete Network - Big data management - Use a function to realize the comparison of two strings, that is, write your own strcmp function, function prototype :int strcmp
Use a function to realize the comparison of two strings, that is, write your own strcmp function, function prototype :int strcmp
First, unconditionally assign a value of 0 to bad;

Second, there is a problem with your judgment condition The condition is changed to (*p == *q ) & & (*p ! = '0') & & (*q! = '0') At this point, enter the loop Execute: p + +, q + +;

while () outside the loop Execute the statement to ask for bad;

Suggestion!

Attached code:

#include<stdio.h>

int str(char* p1,char* p2)

{

while(*p1 && *p2 && *p1 == *p2)

{

p1++;

p2++;

}

return *p1 - *p2;

}

int main(void)

{

char a[100] = "abcdefg";

char b[100] = "abcdasd";

int ret = 0;

ret = str(a,b);

printf("%d\n",ret);

return 0;