本文介绍了需要实现strdup,strnicmp和stricmp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试在操作系统中使用strdup,strnicmp和stricmp,而不是在OS的string.h函数中有一个实现。有人有这些功能的实现吗?你可以发贴它们吗。 谢谢 Jami 解决方案 http://www.debian.org/ http://www.netbsd.org/ http: //www.opensolaris.org/ http://www.freebsd .org / http://www.gentoo.org/ http://www.openbsd.org/ glibc似乎实现了strdup,虽然它不应该对 自己编写。我会从名字中猜出其他两个人应该是 进行与案例无关的比较。循环通过tolower()首先 然后做一个正常的strcmp / strncmp会猜测(虽然 可能更慢,需要一个重复的步骤来做非破坏性的 与区域设置比较相比)。 - imalone jamihuq写道: 这是我不区分大小写的版本的当前形式 strcmp和strncmp: int str_ccmp(const char * s1,const char * s2) { for(;;){ if(* s1!= * s2){ int c1 = toupper((unsigned char)* s1); int c2 = toupper((unsigned char)* s2); if(c2!= c1){ 返回c2> c1? -1:1; } }否则{ if(* s1 ==''\'''){ 返回0; } } ++ s1; ++ s2 ; } } int str_cncmp(const char * s1,const char * s2,size_t n) { for(;;){ if(n-- == 0){ 返回0; } if(* s1!= * s2){ int c1 = toupper((unsigned char)* s1); int c2 = toupper((unsigned char)* s2); if(c2!= c1){ 返回c2> c1? -1:1; } }否则{ if(* s1 ==''\'''){ 返回0; } } ++ s1; ++ s2 ; } } - pete I''m trying to use strdup, strnicmp and stricmp in an OS that doesn''thave an implementation in the OSs string.h function. Does someone havethe implementation for these functions and can you please post them. ThanksJami 解决方案 http://www.debian.org/ http://www.netbsd.org/ http://www.opensolaris.org/ http://www.freebsd.org/ http://www.gentoo.org/ http://www.openbsd.org/ glibc appears to implement strdup, although it shouldn''t be taxing towrite your own. I''ll guess from the names the other two are supposedto be case independent comparisons. Looping through tolower() firstthen doing a normal strcmp/strncmp would work at a guess (althoughprobably slower and needs a duplicating step to do non-destructivelycompared to a locale aware comparison). --imalone This is the current form of my case insensitive versionsof strcmp and strncmp: int str_ccmp(const char *s1, const char *s2){for (;;) {if (*s1 != *s2) {int c1 = toupper((unsigned char)*s1);int c2 = toupper((unsigned char)*s2); if (c2 != c1) {return c2 > c1 ? -1 : 1;}} else {if (*s1 == ''\0'') {return 0;}}++s1;++s2;}} int str_cncmp(const char *s1, const char *s2, size_t n){for (;;) {if (n-- == 0) {return 0;}if (*s1 != *s2) {int c1 = toupper((unsigned char)*s1);int c2 = toupper((unsigned char)*s2); if (c2 != c1) {return c2 > c1 ? -1 : 1;}} else {if (*s1 == ''\0'') {return 0;}}++s1;++s2;}} --pete 这篇关于需要实现strdup,strnicmp和stricmp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 18:25