2010-09-14
函數說明 strcasecmp()用來比較參數s1和s2字串,比較時會自動忽略大小寫差異。
傳回值 若參數s1和s2字串相同則返回0。s1長度大於s2長度則返回大於0 的值,s1
長度若小於s2 長度則返回小於0的值.
#include <stdio.h><br />#include <string.h><br />#include <ctype.h><br />int strcasecmp(const char *s1, const char *s2)<br />{<br /> int c1, c2;<br /> do {<br /> c1 = tolower(*s1++);<br /> c2 = tolower(*s2++);<br /> } while(c1 == c2 && c1 != 0);<br /> return c1 - c2;<br />}<br />int main(void)<br />{<br /> int n = 4;<br /> char str1[] = "Acef";<br /> char str2[] = "ACEFd";<br /> printf("strcasecmp(str1, str2) = %d/n", strcasecmp(str1, str2));<br /> return 0;<br />}
函數說明:strncasecmp()用來比較參數s1和s2字串前n個字元,比較時會自動忽略大小寫差異
傳回值 :若參數s1和s2字串相同則返回0 s1若大於s2則返回大於0的值
s1若小於s2則返回小於0的值
#include <stdio.h><br />#include <string.h><br />#include <ctype.h></p><p>int mystrncasecmp(const char *s1, const char *s2, int n)<br />{<br /> int c1, c2;<br /> do {<br /> c1 = tolower(*s1++);<br /> c2 = tolower(*s2++);<br /> } while((--n > 0) && c1 == c2 && c1 != 0);<br /> return c1 - c2;<br />}<br />int main(void)<br />{<br /> int n = 4;<br /> char str3[] = "ABCf";<br /> char str4[] = "abcd";<br /> printf("mystrncasecmp(str3, str4, n) = %d/n", mystrncasecmp(str3, str4, n));<br /> return 0;<br />}