文字列比較関数
strncmp
ヘッダ #include "string.h"
形 式 int strncmp(const char *s1, const char *s2, size_t n);
機 能 s1が指す文字の配列とs2が指す文字の配列の先頭n文字までの大小関係(先頭から順に1文字ずつ比較していき、異なる文字が出現したときに、それらの文字の対に成立する大小関係とする)の比較を行う。
返却値 等しければ0、s1がs2より大きければ正の整数値、s1がs2より小さければ負の整数値を返す。
■実装例■
int strncmp(const char *s1, const char *s2)
{
while (*s1 == *s2) {
if (*s1 == '\0') /* 等しい */
return (0);
s1++;
s2++;
}
return ((unsigned char)*s1 - (unsigned char)*s2);
}
[Quote]:
http://bohyoh.com