攝影網(wǎng)站網(wǎng)址大全愛站網(wǎng)查詢
深入理解strcmp
?strcmp的主要功能是用來比較兩個字符串
模擬實現(xiàn)strcmp
比較兩個字符串對應(yīng)位置上的大小
按字典序進(jìn)行比較
例如:
輸入:abc abc
輸出:0
輸入:abc ab
輸出:>0的數(shù)
輸入:ab abc
輸出:<0的數(shù)
//深入理解strcmp
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* s1, const char* s2) {assert(s1 != NULL);assert(s2 != NULL);while (*s1 == *s2) {if (*s1 == '\0')return 0;s1++;s2++;}return *s1 - *s2;
}
int main() {int ret=my_strcmp("abc","abc");printf("%d\n", ret);return 0;
}