strcasecmp函數和strncasecmp函數原型
阿新 • • 發佈:2018-03-07
-a har str1 net span bsp -m str2 copy
函數說明 strcasecmp()用來比較參數s1和s2字符串,比較時會自動忽略大小寫的差異。
返回值 若參數s1和s2字符串相同則返回0。s1長度大於s2長度則返回大於0 的值,s1 長度若小於s2 長度則返回小於0的值.
[cpp] view plain copy
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- int strcasecmp(const char *s1, const char *s2)
- {
- int c1, c2;
- do {
- c1 = tolower(*s1++);
- c2 = tolower(*s2++);
- } while(c1 == c2 && c1 != 0);
- return c1 - c2;
- }
- int main(void)
- {
- int n = 4;
- char str1[] = "Acef";
- char str2[] = "ACEFd";
- printf("strcasecmp(str1, str2) = %d/n", strcasecmp(str1, str2));
- return 0;
- }
函數說明:strncasecmp()用來比較參數s1和s2字符串前n個字符,比較時會自動忽略大小寫的差異
返回值 :若參數s1和s2字符串相同則返回0 s1若大於s2則返回大於0的值 s1若小於s2則返回小於0的值
[c-sharp] view plain copy
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- int mystrncasecmp(const char *s1, const char *s2, int n)
- {
- int c1, c2;
- do {
- c1 = tolower(*s1++);
- c2 = tolower(*s2++);
- } while((--n > 0) && c1 == c2 && c1 != 0);
- return c1 - c2;
- }
- int main(void)
- {
- int n = 4;
- char str3[] = "ABCf";
- char str4[] = "abcd";
- printf("mystrncasecmp(str3, str4, n) = %d/n", mystrncasecmp(str3, str4, n));
- return 0;
- }
strcasecmp函數和strncasecmp函數原型