c語言編寫函數(shù)實(shí)現(xiàn)兩個(gè)字符串比較(其功能與標(biāo)準(zhǔn)函數(shù)strcmp一樣)函數(shù)原型int mystrcpy(char*s1,char*s2)其中形參s1,s2分別指向兩個(gè)字符串。如果s1=s2,則返回值為0;如果s1不等于s2則返回它們二者首次遇到的不同字符的ASCII碼的差值。
2 回答

吃雞游戲
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
以下代碼就可解決此問題:
//#include "stdafx.h"//If the vc++6.0, with this line. #include "stdio.h" int mystrcmp( const char *s1, const char *s2){ while (*s1 && *s2 && !(*s1-*s2)) s1++,s2++; return *s1-*s2; } int main( void ){ //測(cè)試一下 char a[]= "12378" ,b[]= "1233467890" ,f; if ((f=mystrcmp(a,b))>0) printf ( "a>b\n" ); else if (f<0) printf ( "a<b\n" ); else printf ( "a=b\n" ); printf ( "\n" ); return 0; } |

慕無忌1623718
TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊
int
mystrcmp(
char
*s1,
char
*s2)
{
while
(*s1 == *s2) {
if
(*s1 ==
'\0'
)
return
0;
s1++;
s2++;
}
return
*s1-*s2;
}
- 2 回答
- 0 關(guān)注
- 193 瀏覽
添加回答
舉報(bào)
0/150
提交
取消