c語言編寫函數(shù)實現(xiàn)兩個字符串比較(其功能與標準函數(shù)strcmp一樣)函數(shù)原型int mystrcpy(char*s1,char*s2)其中形參s1,s2分別指向兩個字符串。如果s1=s2,則返回值為0;如果s1不等于s2則返回它們二者首次遇到的不同字符的ASCII碼的差值。
2 回答
吃雞游戲
TA貢獻1829條經驗 獲得超7個贊
以下代碼就可解決此問題:
//#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){//測試一下 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貢獻1744條經驗 獲得超4個贊
int mystrcmp(char *s1, char *s2){ while(*s1 == *s2) { if(*s1 == '\0') return 0; s1++; s2++; } return *s1-*s2;}
- 2 回答
- 0 關注
- 238 瀏覽
添加回答
舉報
0/150
提交
取消
