本文介绍了strcmpi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用GCC做一个C代码。

我试图比较(case incensitive)两个字符串..但似乎没有

知道 strcmpi ..我使用2个头文件string.h和stdio.h

我该怎么做?


Ex:

#include< stdio.h>

#include< string.h>


int main(无效)

{

char * str =" Case";

char * str1 =" cAsE";


if(strcmpi( str,str1)== 0)

printf(" Same \ n");

else

printf(" Sorry \\ \\ n");

}


Tks


Eduardo Elias Camponez

Hi
Im doing a C code using GCC.
I tried to compare (case Incensitive) two strings.. but it seems dont
"know" strcmpi.. Im using 2 headers string.h and stdio.h
How can I do this?

Ex:
#include <stdio.h>
#include <string.h>

int main (void)
{
char * str = "Case";
char * str1 = "cAsE";

if (strcmpi(str,str1)==0)
printf("Same\n");
else
printf("Sorry\n");
}

Tks

Eduardo Elias Camponez

推荐答案




我相信strcmpi是一个实现定义的头。尝试在手册页中查找

。在VC ++下,它使用_strcmpi()调用,因此可能类似于你的情况。

Allan



I believe strcmpi is an implementation-defined header. Try looking for it
in the man pages. Under VC++ it is called using _strcmpi() so may be
similar for your case.
Allan





你可能有

strcasecmp()



you probably have
strcasecmp()





只需提供你自己的版本``strcmpi''''..虽然我认为你可能会想要重命名它,因为str *是由实现保留的,如果我不是

错了。


----------------------------

#include< ctype.h>


int stricmp(char * s1,char * s2)

{

for (; * s1&& * s2&&(toupper(* s1)== toupper(* s2)); ++ s1,++ s2);


返回* s1 - * s2;

}

----------------------------


也许将其名称更改为cmpistr ?避免调用未定义的行为。



Just provide your own version of ``strcmpi'''' .. although I think you might
want to rename it, since str* is reserved by the implementation if I am not
mistaken.

----------------------------
#include <ctype.h>

int stricmp(char *s1, char *s2)
{
for (; *s1 && *s2 && (toupper(*s1) == toupper(*s2)); ++s1, ++s2);

return *s1 - *s2;
}
----------------------------

Maybe change its name to cmpistr? to avoid invoking undefined behaviour.


这篇关于strcmpi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 12:53