1. 程式人生 > >strcasecmp函數和strncasecmp函數原型

strcasecmp函數和strncasecmp函數原型

-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
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. int strcasecmp(const char *s1, const char *s2)
  5. {
  6. int c1, c2;
  7. do {
  8. c1 = tolower(*s1++);
  9. c2 = tolower(*s2++);
  10. } while(c1 == c2 && c1 != 0);
  11. return c1 - c2;
  12. }
  13. int main(void)
  14. {
  15. int n = 4;
  16. char str1[] = "Acef";
  17. char str2[] = "ACEFd";
  18. printf("strcasecmp(str1, str2) = %d/n", strcasecmp(str1, str2));
  19. return 0;
  20. }

函數說明:strncasecmp()用來比較參數s1和s2字符串前n個字符,比較時會自動忽略大小寫的差異

返回值 :若參數s1和s2字符串相同則返回0 s1若大於s2則返回大於0的值 s1若小於s2則返回小於0的值

[c-sharp] view plain copy
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. int mystrncasecmp(const char *s1, const char *s2, int n)
  5. {
  6. int c1, c2;
  7. do {
  8. c1 = tolower(*s1++);
  9. c2 = tolower(*s2++);
  10. } while((--n > 0) && c1 == c2 && c1 != 0);
  11. return c1 - c2;
  12. }
  13. int main(void)
  14. {
  15. int n = 4;
  16. char str3[] = "ABCf";
  17. char str4[] = "abcd";
  18. printf("mystrncasecmp(str3, str4, n) = %d/n", mystrncasecmp(str3, str4, n));
  19. return 0;
  20. }

strcasecmp函數和strncasecmp函數原型