请输入您要查询的百科知识:

 

词条 strnicmp
释义

函数名: strnicmp

功 能: 比较字符串str1和str2的前n个字符串字典序的大小,但是不区分字母大小写。

返回值: 当str1<str2时,返回值<0 ; 当str1=str2时,返回值=0; 当str1>str2时,返回值>0。

比较是这样进行的,先比较2个字符串的第1个字符字典序的大小,如果能比较出大小,则马上返回了,如果不能区别大小,开始比较第2个,如果这时第1个字符串已经到尽头了,第2个字符串还有字符,这时算第2个字符串大。例:

char *str1="B";

char *str2="abcD";

int n=4;

strnicmp(char *str1, char *str2, 4);

结果是str1大,因为B在a的后面。

char *str1="ABCD";

char *str2="abcD";

int n=4;

strnicmp(char *str1, char *str2, 4);

结果一样大。

char *str1="abc";

char *str2="abcD";

int n=5;

strnicmp(char *str1, char *str2, 5);

结果是str2大。

可以把上述数据,填到下面程序中一试便知道。

用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);

程序例:

#include <string.h>

#include <stdio.h>

int main(void)

{

char *buf1 = "BBBccc", *buf2 = "bbbccc";

int nResult;

nResult = strnicmp(buf2, buf1, 3);

if (nResult > 0)

printf("buffer 2 is greater than buffer 1\");

if (nResult < 0)

printf("buffer 2 is less than buffer 1\");

if (nResult == 0)

printf("buffer 2 equals buffer 1\");

return 0;

}

//---------------------------------

还有两种情况: (n1 = 3 或 n2= 2时)

char *str1="aBc";

char *str2="abcD";

strnicmp(char *str1, char *str2, 3);

strnicmp(char *str1, char *str2, 2);

两种情况的结果是否应该等于 0 呢?

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/3/20 4:40:34