Ïà¹Øº¯Êý bcmp£¬strcasecmp£¬strcmp£¬strcoll£¬strncmp£¬strncasecmp
±íÍ·Îļþ #include
¶¨Ò庯Êý int bcmp ( const void *s1,const void * s2,int n);
º¯Êý˵Ã÷ bcmp()ÓÃÀ´±È½Ïs1ºÍs2ËùÖ¸µÄÄÚ´æÇø¼äÇ°n¸ö×Ö½Ú£¬Èô²ÎÊýnΪ0£¬Ôò·µ»Ø0¡£
·µ»ØÖµ Èô²ÎÊýs1 ºÍs2 ËùÖ¸µÄÄÚ´æÄÚÈݶ¼ÍêÈ«ÏàͬÔò·µ»Ø0 Öµ£¬·ñÔò·µ»Ø·ÇÁãÖµ¡£
¸½¼Ó˵Ã÷ ½¨ÒéʹÓÃmemcmp()È¡´ú¡£
·¶Àý ²Î¿¼memcmp()¡£
¡¡
bcopy£¨¿½±´ÄÚ´æÄÚÈÝ£©
Ïà¹Øº¯Êý memccpy£¬memcpy£¬memmove£¬strcpy£¬ctrncpy
±íÍ·Îļþ #include
¶¨Ò庯Êý void bcopy ( const void *src,void *dest ,int n);
º¯Êý˵Ã÷ bcopy()Óëmemcpy()Ò»Ñù¶¼ÊÇÓÃÀ´¿½±´srcËùÖ¸µÄÄÚ´æÄÚÈÝÇ°n¸ö×Ö½Úµ½destËùÖ¸µÄµØÖ·£¬²»¹ý²ÎÊýsrcÓëdestÔÚ´«¸øº¯ÊýʱÊÇÏà·´µÄλÖá£
·µ»ØÖµ
¸½¼Ó˵Ã÷ ½¨ÒéʹÓÃmemcpy()È¡´ú
·¶Àý #include
main()
{
char dest[30]=¡±string(a)¡±;
char src[30]=¡±string\0string¡±;
int i;
bcopy(src,dest,30);/* srcÖ¸Õë·ÅÔÚÇ°*/
printf(bcopy(): ¡°)
for(i=0;i<30;i++)
printf(¡°%c¡±,dest);
memcpy(dest src,30); /*destÖ¸Õë·ÅÔÚÇ®*/
printf(¡®\nmemcpy() : ¡°);
for(i=0;i<30;i++)
printf(¡°%c¡±,dest);
Ö´ÐÐ bcopy() : string string
memcpy() :string sring
¡¡
bzero£¨½«Ò»¶ÎÄÚ´æÄÚÈÝÈ«ÇåΪÁ㣩
Ïà¹Øº¯Êý memset£¬swab
±íÍ·Îļþ #include
¶¨Ò庯Êý void bzero(void *s,int n)£»
º¯Êý˵Ã÷ bzero()»á½«²ÎÊýsËùÖ¸µÄÄÚ´æÇøÓòÇ°n¸ö×Ö½Ú£¬È«²¿ÉèΪÁãÖµ¡£Ï൱ÓÚµ÷ÓÃmemset((void*)s,0,size_tn);
·µ»ØÖµ
¸½¼Ó˵Ã÷ ½¨ÒéʹÓÃmemsetÈ¡´ú
·¶Àý ²Î¿¼memset()¡£
¡¡
index£¨²éÕÒ×Ö·û´®ÖеÚÒ»¸ö³öÏÖµÄÖ¸¶¨×Ö·û£©
Ïà¹Øº¯Êý rindex£¬srechr£¬strrchr
±íÍ·Îļþ #include
¶¨Ò庯Êý char * index( const char *s, int c);
º¯Êý˵Ã÷ index()ÓÃÀ´ÕÒ³ö²ÎÊýs×Ö·û´®ÖеÚÒ»¸ö³öÏֵIJÎÊýcµØÖ·£¬È»ºó½«¸Ã×Ö·û³öÏֵĵØÖ··µ»Ø¡£×Ö·û´®½áÊø×Ö·û(NULL)Ò²ÊÓΪ×Ö·û´®Ò»²¿·Ö¡£
·µ»ØÖµ Èç¹ûÕÒµ½Ö¸¶¨µÄ×Ö·ûÔò·µ»Ø¸Ã×Ö·ûËùÔÚµØÖ·£¬·ñÔò·µ»Ø0¡£
·¶Àý #include
main()
{
char *s =¡±0123456789012345678901234567890¡±;
char *p;
p =index(s,¡¯5¡¯);
printf(%s\n¡±,p);
}
Ö´ÐÐ 5.68E+25
¡¡
memccpy£¨¿½±´ÄÚ´æÄÚÈÝ£©
Ïà¹Øº¯Êý bcopy£¬memcpy£¬memmove£¬strcpy£¬strncpy
±íÍ·Îļþ #include
¶¨Ò庯Êý void * memccpy(void *dest, const void * src, int c,size_t n);
º¯Êý˵Ã÷ memccpy()ÓÃÀ´¿½±´srcËùÖ¸µÄÄÚ´æÄÚÈÝÇ°n¸ö×Ö½Úµ½destËùÖ¸µÄµØÖ·ÉÏ¡£Óëmemcpy()²»Í¬µÄÊÇ£¬memccpy()»áÔÚ¸´ÖÆʱ¼ì²é²ÎÊýcÊÇ·ñ³öÏÖ£¬ÈôÊÇÔò·µ»ØdestÖÐֵΪcµÄÏÂÒ»¸ö×Ö½ÚµØÖ·¡£
·µ»ØÖµ ·µ»ØÖ¸ÏòdestÖÐֵΪcµÄÏÂÒ»¸ö×Ö½ÚÖ¸Õë¡£·µ»ØֵΪ0±íʾÔÚsrcËùÖ¸ÄÚ´æÇ°n¸ö×Ö½ÚÖÐûÓÐֵΪcµÄ×Ö½Ú¡£
·¶Àý #include
main()
{
char a[]="string[a]";
char b[]="string(b)";
memccpy(a,b,'B',sizeof(b));
printf("memccpy():%s\n",a);
}
Ö´ÐÐ memccpy():string(b)
¡¡
memchr£¨ÔÚijһÄڴ淶ΧÖвéÕÒÒ»Ìض¨×Ö·û£©
Ïà¹Øº¯Êý index£¬rindex£¬strchr£¬strpbrk£¬strrchr£¬strsep£¬strspn£¬strstr
±íÍ·Îļþ #include
¶¨Ò庯Êý void * memchr(const void *s,int c,size_t n);
º¯Êý˵Ã÷ memchr()´ÓÍ·¿ªÊ¼ËÑÑ°sËùÖ¸µÄÄÚ´æÄÚÈÝÇ°n¸ö×Ö½Ú£¬Ö±µ½·¢ÏÖµÚÒ»¸öֵΪcµÄ×Ö½Ú£¬Ôò·µ»ØÖ¸Ïò¸Ã×Ö½ÚµÄÖ¸Õë¡£
·µ»ØÖµ Èç¹ûÕÒµ½Ö¸¶¨µÄ×Ö½ÚÔò·µ»Ø¸Ã×Ö½ÚµÄÖ¸Õ룬·ñÔò·µ»Ø0¡£
·¶Àý #include
main()
{
char *s="0123456789012345678901234567890";
char *p;
p=memchr(s,'5',10);
printf("%s\n",p);
}
Ö´ÐÐ 5.68E+25
¡¡
memcmp£¨±È½ÏÄÚ´æÄÚÈÝ£©
Ïà¹Øº¯Êý bcmp£¬strcasecmp£¬strcmp£¬strcoll£¬strncmp£¬strncasecmp
±íÍ·Îļþ #include
¶¨Ò庯Êý int memcmp (const void *s1,const void *s2,size_t n);
º¯Êý˵Ã÷ memcmp()ÓÃÀ´±È½Ïs1ºÍs2ËùÖ¸µÄÄÚ´æÇø¼äÇ°n¸ö×Ö·û¡£×Ö·û´®´óСµÄ±È½ÏÊÇÒÔASCIIÂë±íÉϵÄ˳ÐòÀ´¾ö¶¨£¬´Î˳ÐòÒàΪ×Ö·ûµÄÖµ¡£memcmp()Ê×ÏȽ«s1µÚÒ»¸ö×Ö·ûÖµ¼õÈ¥s2µÚÒ»¸ö×Ö·ûµÄÖµ£¬Èô²îΪ0ÔòÔÙ¼ÌÐø±È½Ïϸö×Ö·û£¬Èô²îÖµ²»Îª0Ôò½«²îÖµ·µ»Ø¡£ÀýÈ磬×Ö·û´®"Ac"ºÍ"ba"±È½ÏÔò»á·µ»Ø×Ö·û'A'(65)ºÍ'b'(98)µÄ²îÖµ(£33)¡£
·µ»ØÖµ Èô²ÎÊýs1ºÍs2ËùÖ¸µÄÄÚ´æÄÚÈݶ¼ÍêÈ«ÏàͬÔò·µ»Ø0Öµ¡£s1Èô´óÓÚs2Ôò·µ»Ø´óÓÚ0µÄÖµ¡£s1ÈôСÓÚs2Ôò·µ»ØСÓÚ0µÄÖµ¡£
·¶Àý #include
main()
{
char *a ="aBcDeF";
char *b="AbCdEf";
char *c="aacdef";
char *d="aBcDeF";
printf("memcmp(a,b):%d\n",memcmp((void*)a,(void*) b,6));
printf("memcmp(a,c):%d\n",memcmp((void*)a,(void*) c,6));
printf("memcmp(a,d):%d\n",memcmp((void*)a,(void*) d,6));
Ö´ÐÐ memcmp(a,b):1 /*×Ö·û´®a>×Ö·û´®b£¬·µ»Ø1*/
memcmp(a,c):-1 /* ×Ö·û´®a<×Ö·û´®c,·µ»Ø£1*/
memcmp(a,d):0 /*×Ö·û´®a£½×Ö·û´®d£¬·µ»Ø0*/
¡¡
memcpy£¨¿½±´ÄÚ´æÄÚÈÝ£©
Ïà¹Øº¯Êý bcopy£¬memccpy£¬memcpy£¬memmove£¬strcpy£¬strncpy
±íÍ·Îļþ #include
¶¨Ò庯Êý void * memcpy (void * dest ,const void *src, size_t n);
º¯Êý˵Ã÷ memcpy()ÓÃÀ´¿½±´srcËùÖ¸µÄÄÚ´æÄÚÈÝÇ°n¸ö×Ö½Úµ½destËùÖ¸µÄÄÚ´æµØÖ·ÉÏ¡£Óëstrcpy()²»Í¬µÄÊÇ£¬memcpy()»áÍêÕûµÄ¸´ÖÆn¸ö×Ö½Ú£¬²»»áÒòΪÓöµ½×Ö·û´®½áÊø'\0'¶ø½áÊø¡£
·µ»ØÖµ ·µ»ØÖ¸ÏòdestµÄÖ¸Õë¡£
¸½¼Ó˵Ã÷ Ö¸ÕësrcºÍdestËùÖ¸µÄÄÚ´æÇøÓò²»¿ÉÖصþ¡£
·¶Àý #include
main()
{
char a[30]="string (a)";
char b[30]="string\0string";
int i;
strcpy(a,b);
printf("strcpy():");
for(i=0;i<30;i++)
printf("%c",a);
memcpy(a,b,30);
printf("\nmemcpy() :");
for(i=0;i<30;i++)
printf("%c",a);
}
Ö´ÐÐ strcpy() : string a )
memcpy() : string string
¡¡
memmove£¨¿½±´ÄÚ´æÄÚÈÝ£©
Ïà¹Øº¯Êý bcopy£¬memccpy£¬memcpy£¬strcpy£¬strncpy
±íÍ·Îļþ #include
¶¨Ò庯Êý void * memmove(void *dest,const void *src,size_t n);
º¯Êý˵Ã÷ memmove()Óëmemcpy()Ò»Ñù¶¼ÊÇÓÃÀ´¿½±´srcËùÖ¸µÄÄÚ´æÄÚÈÝÇ°n¸ö×Ö½Úµ½destËùÖ¸µÄµØÖ·ÉÏ¡£²»Í¬µÄÊÇ£¬µ±srcºÍdestËùÖ¸µÄÄÚ´æÇøÓòÖصþʱ£¬memmove()ÈÔÈ»¿ÉÒÔÕýÈ·µÄ´¦Àí£¬²»¹ýÖ´ÐÐЧÂÊÉÏ»á±ÈʹÓÃmemcpy()ÂÔÂýЩ¡£
·µ»ØÖµ ·µ»ØÖ¸ÏòdestµÄÖ¸Õë¡£
¸½¼Ó˵Ã÷ Ö¸ÕësrcºÍdestËùÖ¸µÄÄÚ´æÇøÓò¿ÉÒÔÖصþ¡£
·¶Àý ²Î¿¼memcpy()¡£
¡¡
memset£¨½«Ò»¶ÎÄÚ´æ¿Õ¼äÌîÈëijֵ£©
Ïà¹Øº¯Êý bzero£¬swab
±íÍ·Îļþ #include
¶¨Ò庯Êý void * memset (void *s ,int c, size_t n);
º¯Êý˵Ã÷ memset()»á½«²ÎÊýsËùÖ¸µÄÄÚ´æÇøÓòÇ°n¸ö×Ö½ÚÒÔ²ÎÊýcÌîÈ룬Ȼºó·µ»ØÖ¸ÏòsµÄÖ¸Õë¡£ÔÚ±àд³ÌÐòʱ£¬ÈôÐèÒª½«Ä³Ò»Êý×é×÷³õʼ»¯£¬memset()»áÏ൱·½±ã¡£
·µ»ØÖµ ·µ»ØÖ¸ÏòsµÄÖ¸Õë¡£
¸½¼Ó˵Ã÷ ²ÎÊýcËäÉùÃ÷Ϊint£¬ µ«±ØÐëÊÇunsigned char £¬ËùÒÔ·¶Î§ÔÚ0µ½255Ö®¼ä¡£
·¶Àý #include
main()
{
char s[30];
memset (s,'A',sizeof(s));
s[30]='\0';
printf("%s\n",s);
}
Ö´ÐÐ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
¡¡
rindex£¨²éÕÒ×Ö·û´®ÖÐ×îºóÒ»¸ö³öÏÖµÄÖ¸¶¨×Ö·û£©
Ïà¹Øº¯Êý index£¬memchr£¬strchr£¬strrchr
±íÍ·Îļþ #include
¶¨Ò庯Êý char * rindex( const char *s,int c);
º¯Êý˵Ã÷ rindex()ÓÃÀ´ÕÒ³ö²ÎÊýs×Ö·û´®ÖÐ×îºóÒ»¸ö³öÏֵIJÎÊýcµØÖ·£¬È»ºó½«¸Ã×Ö·û³öÏֵĵØÖ··µ»Ø¡£×Ö·û´®½áÊø×Ö·û(NULL)Ò²ÊÓΪ×Ö·û´®Ò»²¿·Ö¡£
·µ»ØÖµ Èç¹ûÕÒµ½Ö¸¶¨µÄ×Ö·ûÔò·µ»Ø¸Ã×Ö·ûËùÔڵĵØÖ·£¬·ñÔò·µ»Ø0¡£
·¶Àý #include
mian()
{
char *s ="0123456789012345678901234567890";
char *p;
p=rindex(s,'5');
printf("%s\n",p);
}
Ö´ÐÐ 567890
¡¡
strcasecmp£¨ºöÂÔ´óСд±È½Ï×Ö·û´®£©
Ïà¹Øº¯Êý bcmp£¬memcmp£¬strcmp£¬strcoll£¬strncmp
±íÍ·Îļþ #include
¶¨Ò庯Êý int strcasecmp (const char *s1, const char *s2);
º¯Êý˵Ã÷ strcasecmp()ÓÃÀ´±È½Ï²ÎÊýs1ºÍs2×Ö·û´®£¬±È½Ïʱ»á×Ô¶¯ºöÂÔ´óСдµÄ²îÒì¡£
·µ»ØÖµ Èô²ÎÊýs1ºÍs2×Ö·û´®ÏàͬÔò·µ»Ø0¡£s1³¤¶È´óÓÚs2³¤¶ÈÔò·µ»Ø´óÓÚ0 µÄÖµ£¬s1 ³¤¶ÈÈôСÓÚs2 ³¤¶ÈÔò·µ»ØСÓÚ0µÄÖµ¡£
·¶Àý #include
main()
{
char *a="aBcDeF";
char *b="AbCdEf";
if(!strcasecmp(a,b))
printf("%s=%s\n",a,b);
}
Ö´ÐÐ aBcDeF=AbCdEf
¡¡
strcat£¨Á¬½ÓÁ½×Ö·û´®£©
Ïà¹Øº¯Êý bcopy£¬memccpy£¬memcpy£¬strcpy£¬strncpy
±íÍ·Îļþ #include
¶¨Ò庯Êý char *strcat (char *dest,const char *src);
º¯Êý˵Ã÷ strcat()»á½«²ÎÊýsrc×Ö·û´®¿½±´µ½²ÎÊýdestËùÖ¸µÄ×Ö·û´®Î²¡£µÚÒ»¸ö²ÎÊýdestÒªÓÐ×ã¹»µÄ¿Õ¼äÀ´ÈÝÄÉÒª¿½±´µÄ×Ö·û´®¡£
·µ»ØÖµ ·µ»Ø²ÎÊýdestµÄ×Ö·û´®ÆðʼµØÖ·
·¶Àý #include
main()
{
char a[30]="string(1)";
char b[]="string(2)";
printf("before strcat() : %s\n",a);
printf("after strcat() : %s\n",strcat(a,b));
}
Ö´ÐÐ before strcat () : string(1)
after strcat () : string(1)string(2)
¡¡
strchr£¨²éÕÒ×Ö·û´®ÖеÚÒ»¸ö³öÏÖµÄÖ¸¶¨×Ö·û£©
Ïà¹Øº¯Êý index£¬memchr£¬rinex£¬strbrk£¬strsep£¬strspn£¬strstr£¬strtok
±íÍ·Îļþ #include
¶¨Ò庯Êý char * strchr (const char *s,int c);
º¯Êý˵Ã÷ strchr()ÓÃÀ´ÕÒ³ö²ÎÊýs×Ö·û´®ÖеÚÒ»¸ö³öÏֵIJÎÊýcµØÖ·£¬È»ºó½«¸Ã×Ö·û³öÏֵĵØÖ··µ»Ø¡£
·µ»ØÖµ Èç¹ûÕÒµ½Ö¸¶¨µÄ×Ö·ûÔò·µ»Ø¸Ã×Ö·ûËùÔÚµØÖ·£¬·ñÔò·µ»Ø0¡£
·¶Àý #include
main()
{
char *s=0123456789012345678901234567890¡±;
char *p;
p=strchr(s,'5');
printf("%s\n",p);
}
Ö´ÐÐ 5.68E+25
¡¡
strcmp£¨±È½Ï×Ö·û´®£©
Ïà¹Øº¯Êý bcmp£¬memcmp£¬strcasecmp£¬strncasecmp£¬strcoll
±íÍ·Îļþ #include
¶¨Ò庯Êý int strcmp(const char *s1,const char *s2);
º¯Êý˵Ã÷ strcmp()ÓÃÀ´±È½Ï²ÎÊýs1ºÍs2×Ö·û´®¡£×Ö·û´®´óСµÄ±È½ÏÊÇÒÔASCII Âë±íÉϵÄ˳ÐòÀ´¾ö¶¨£¬´Ë˳ÐòÒàΪ×Ö·ûµÄÖµ¡£strcmp()Ê×ÏȽ«s1µÚÒ»¸ö×Ö·ûÖµ¼õÈ¥s2µÚÒ»¸ö×Ö·ûÖµ£¬Èô²îֵΪ0ÔòÔÙ¼ÌÐø±È½Ïϸö×Ö·û£¬Èô²îÖµ²»Îª0Ôò½«²îÖµ·µ»Ø¡£ÀýÈç×Ö·û´®"Ac"ºÍ"ba"±È½ÏÔò»á·µ»Ø×Ö·û"A"(65)ºÍ'b'(98)µÄ²îÖµ(£33)¡£
·µ»ØÖµ Èô²ÎÊýs1ºÍs2×Ö·û´®ÏàͬÔò·µ»Ø0¡£s1Èô´óÓÚs2Ôò·µ»Ø´óÓÚ0µÄÖµ¡£s1ÈôСÓÚs2Ôò·µ»ØСÓÚ0 µÄÖµ¡£
·¶Àý #include
main()
{
char *a="aBcDeF";
char *b="AbCdEf";
char *c="aacdef";
char *d="aBcDeF";
printf("strcmp(a,b) : %d\n",strcmp(a,b));
printf("strcmp(a,c) : %d\n",strcmp(a,c));
printf("strcmp(a,d) : %d\n",strcmp(a,d));
}
Ö´ÐÐ strcmp(a,b) : 32
strcmp(a,c) :-31
strcmp(a,d) : 0
¡¡
strcoll£¨²ÉÓÃÄ¿Ç°ÇøÓòµÄ×Ö·ûÅÅÁдÎÐòÀ´±È½Ï×Ö·û´®£©
Ïà¹Øº¯Êý strcmp£¬bcmp£¬memcmp£¬strcasecmp£¬strncasecmp
±íÍ·Îļþ #include
¶¨Ò庯Êý int strcoll( const char *s1, const char *s2);
º¯Êý˵Ã÷ strcoll()»áÒÀ»·¾³±äÁ¿LC_COLLATEËùÖ¸¶¨µÄÎÄ×ÖÅÅÁдÎÐòÀ´±È½Ïs1ºÍs2 ×Ö·û´®¡£
·µ»ØÖµ Èô²ÎÊýs1ºÍs2×Ö·û´®ÏàͬÔò·µ»Ø0¡£s1Èô´óÓÚs2Ôò·µ»Ø´óÓÚ0µÄÖµ¡£s1ÈôСÓÚs2Ôò·µ»ØСÓÚ0 µÄÖµ¡£
¸½¼Ó˵Ã÷ ÈôLC_COLLATEΪ"POSIX"»ò"C"£¬Ôòstrcoll()Óëstrcmp()×÷ÓÃÍêÈ«Ïàͬ¡£
·¶Àý ²Î¿¼strcmp()¡£
¡¡
strcpy£¨¿½±´×Ö·û´®£©
Ïà¹Øº¯Êý bcopy£¬memcpy£¬memccpy£¬memmove
±íÍ·Îļþ #include
¶¨Ò庯Êý char *strcpy(char *dest,const char *src);
º¯Êý˵Ã÷ strcpy()»á½«²ÎÊýsrc×Ö·û´®¿½±´ÖÁ²ÎÊýdestËùÖ¸µÄµØÖ·¡£
·µ»ØÖµ ·µ»Ø²ÎÊýdestµÄ×Ö·û´®ÆðʼµØÖ·¡£
¸½¼Ó˵Ã÷ Èç¹û²ÎÊýdestËùÖ¸µÄÄÚ´æ¿Õ¼ä²»¹»´ó£¬¿ÉÄÜ»áÔì³É»º³åÒç³ö(buffer Overflow)µÄ´íÎóÇé¿ö£¬ÔÚ±àд³ÌÐòʱÇëÌرðÁôÒ⣬»òÕßÓÃstrncpy()À´È¡´ú¡£
·¶Àý #include
main()
{
char a[30]="string(1)";
char b[]="string(2)";
printf("before strcpy() :%s\n",a);
printf("after strcpy() :%s\n",strcpy(a,b));
}
Ö´ÐÐ before strcpy() :string(1)
after strcpy() :string(2)
¡¡
strcspn£¨·µ»Ø×Ö·û´®ÖÐÁ¬Ðø²»º¬Ö¸¶¨×Ö·û´®ÄÚÈݵÄ×Ö·ûÊý£©
Ïà¹Øº¯Êý strspn
±íÍ·Îļþ #inclued
¶¨Ò庯Êý size_t strcspn ( const char *s,const char * reject);
º¯Êý˵Ã÷ strcspn()´Ó²ÎÊýs×Ö·û´®µÄ¿ªÍ·¼ÆËãÁ¬ÐøµÄ×Ö·û£¬¶øÕâЩ×Ö·û¶¼ÍêÈ«²»ÔÚ²ÎÊýreject ËùÖ¸µÄ×Ö·û´®ÖС£¼òµ¥µØ˵£¬Èôstrcspn()·µ»ØµÄÊýֵΪn£¬Ôò´ú±í×Ö·û´®s¿ªÍ·Á¬ÐøÓÐn¸ö×Ö·û¶¼²»º¬×Ö·û´®rejectÄÚµÄ×Ö·û¡£
·µ»ØÖµ ·µ»Ø×Ö·û´®s¿ªÍ·Á¬Ðø²»º¬×Ö·û´®rejectÄÚµÄ×Ö·ûÊýÄ¿¡£
·¶Àý #include
main()
{
char *str="Linux was first developed for 386/486-based pcs.";
printf("%d\n",strcspn(str," "));
printf("%d\n",strcspn(str,"/-"));
printf("%d\n",strcspn(str,"1234567890"));
}
Ö´ÐÐ 5 /*Ö»¼ÆËãµ½¡° ¡±µÄ³öÏÖ£¬ËùÒÔ·µ»Ø¡°Linux¡±µÄ³¤¶È*/
33 /*¼ÆËãµ½³öÏÖ¡°/¡±»ò¡°£¡±£¬ËùÒÔ·µ»Øµ½¡°6¡±µÄ³¤¶È*/
30 /* ¼ÆËãµ½³öÏÖÊý×Ö×Ö·ûΪֹ£¬ËùÒÔ·µ»Ø¡°3¡±³öÏÖÇ°µÄ³¤¶È*/
¡¡
strdup£¨¸´ÖÆ×Ö·û´®£©
Ïà¹Øº¯Êý calloc£¬malloc£¬realloc£¬free
±íÍ·Îļþ #include
¶¨Ò庯Êý char * strdup( const char *s);
º¯Êý˵Ã÷ strdup()»áÏÈÓÃmaolloc()ÅäÖÃÓë²ÎÊýs×Ö·û´®ÏàͬµÄ¿Õ¼ä´óС£¬È»ºó½«²ÎÊýs×Ö·û´®µÄÄÚÈݸ´ÖƵ½¸ÃÄÚ´æµØÖ·£¬È»ºó°Ñ¸ÃµØÖ··µ»Ø¡£¸ÃµØÖ·×îºó¿ÉÒÔÀûÓÃfree()À´ÊÍ·Å¡£
·µ»ØÖµ ·µ»ØÒ»×Ö·û´®Ö¸Õ룬¸ÃÖ¸ÕëÖ¸Ïò¸´ÖƺóµÄÐÂ×Ö·û´®µØÖ·¡£Èô·µ»ØNULL±íʾÄÚ´æ²»×ã¡£
·¶Àý #include
main()
{
char a[]="strdup";
char *b;
b=strdup(a);
printf("b[ ]=\"%s\"\n",b);
}
Ö´ÐÐ b[ ]="strdup"
¡¡
strlen£¨·µ»Ø×Ö·û´®³¤¶È£©
Ïà¹Øº¯Êý
±íÍ·Îļþ #include
¶¨Ò庯Êý size_t strlen (const char *s);
º¯Êý˵Ã÷ strlen()ÓÃÀ´¼ÆËãÖ¸¶¨µÄ×Ö·û´®sµÄ³¤¶È£¬²»°üÀ¨½áÊø×Ö·û"\0"¡£
·µ»ØÖµ ·µ»Ø×Ö·û´®sµÄ×Ö·ûÊý¡£
·¶Àý /*È¡µÃ×Ö·û´®strµÄ³¤¶È*/
#include
main()
{
char *str = "12345678";
printf("str length = %d\n", strlen(str));
}
Ö´ÐÐ str length = 8
¡¡
strncasecmp£¨ºöÂÔ´óСд±È½Ï×Ö·û´®£©
Ïà¹Øº¯Êý bcmp£¬memcmp£¬strcmp£¬strcoll£¬strncmp
±íÍ·Îļþ #include
¶¨Ò庯Êý int strncasecmp(const char *s1,const char *s2,size_t n);
º¯Êý˵Ã÷ strncasecmp()ÓÃÀ´±È½Ï²ÎÊýs1ºÍs2×Ö·û´®Ç°n¸ö×Ö·û£¬±È½Ïʱ»á×Ô¶¯ºöÂÔ´óСдµÄ²îÒì¡£
·µ»ØÖµ Èô²ÎÊýs1ºÍs2 ×Ö·û´®ÏàͬÔò·µ»Ø0¡£s1 Èô´óÓÚs2Ôò·µ»Ø´óÓÚ0µÄÖµ£¬s1ÈôСÓÚs2Ôò·µ»ØСÓÚ0 µÄÖµ¡£
·¶Àý #include
main()
{
char *a="aBcDeF";
char *b="AbCdEf";
if(!strncasecmp(a,b))
printf("%s =%s\n",a,b);
}
Ö´ÐÐ aBcDef=AbCdEf
¡¡
strncat£¨Á¬½ÓÁ½×Ö·û´®£©
Ïà¹Øº¯Êý bcopy£¬memccpy£¬memecpy£¬strcpy£¬strncpy
±íÍ·Îļþ #inclue
¶¨Ò庯Êý char * strncat(char *dest,const char *src,size_t n);
º¯Êý˵Ã÷ strncat()»á½«²ÎÊýsrc×Ö·û´®¿½±´n¸ö×Ö·ûµ½²ÎÊýdestËùÖ¸µÄ×Ö·û´®Î²¡£µÚÒ»¸ö²ÎÊýdestÒªÓÐ×ã¹»µÄ¿Õ¼äÀ´ÈÝÄÉÒª¿½±´µÄ×Ö·û´®¡£
·µ»ØÖµ ·µ»Ø²ÎÊýdestµÄ×Ö·û´®ÆðʼµØÖ·¡£
·¶Àý #include
main()
{
char a[30]="string(1)";
char b[]="string(2)";
printf("before strnact() :%s\n", a);
printf("after strncat() :%s\n", strncat(a,b,6));
}
Ö´ÐÐ before strnact() : string(1)
after strncat() : string(1) string
¡¡
strncpy£¨¿½±´×Ö·û´®£©
Ïà¹Øº¯Êý bcopy£¬memccpy£¬memcpy£¬memmove
±íÍ·Îļþ #include
¶¨Ò庯Êý char * strncpy(char *dest,const char *src,size_t n);
º¯Êý˵Ã÷ strncpy()»á½«²ÎÊýsrc×Ö·û´®¿½±´Ç°n¸ö×Ö·ûÖÁ²ÎÊýdestËùÖ¸µÄµØÖ·¡£
·µ»ØÖµ ·µ»Ø²ÎÊýdestµÄ×Ö·û´®ÆðʼµØÖ·¡£
·¶Àý #inclue
main()
{
char a[30]="string(1)";
char b[]="string(2)";
printf("before strncpy() : %s\n",a);
printf("after strncpy() : %s\n",strncpy(a,b,6));
}
Ö´ÐÐ before strncpy() : string(1)
after strncpy() : string(1)
¡¡
strpbrk£¨²éÕÒ×Ö·û´®ÖеÚÒ»¸ö³öÏÖµÄÖ¸¶¨×Ö·û£©
Ïà¹Øº¯Êý index£¬memchr£¬rindex£¬strpbrk£¬strsep£¬strspn£¬strstr£¬strtok
±íÍ·Îļþ #include
¶¨Ò庯Êý char *strpbrk(const char *s,const char *accept);
º¯Êý˵Ã÷ strpbrk()ÓÃÀ´ÕÒ³ö²ÎÊýs ×Ö·û´®ÖÐ×îÏȳöÏÖ´æÔÚ²ÎÊýaccept ×Ö·û´®ÖеÄÈÎÒâ×Ö·û¡£
·µ»ØÖµ Èç¹ûÕÒµ½Ö¸¶¨µÄ×Ö·ûÔò·µ»Ø¸Ã×Ö·ûËùÔÚµØÖ·£¬·ñÔò·µ»Ø0¡£
·¶Àý #include
main()
{
char *s="0123456789012345678901234567890";
char *p;
p=strpbrk(s,"a1 839"); /*1»á×îÏÈÔÚs×Ö·û´®ÖÐÕÒµ½*/
printf("%s\n",p);
p=strprk(s,"4398");/*3 »á×îÏÈÔÚs ×Ö·û´®ÖÐÕÒµ½*/
printf("%s\n",p);
Ö´ÐÐ 1.23E+29
¡¡
strrchr£¨²éÕÒ×Ö·û´®ÖÐ×îºó³öÏÖµÄÖ¸¶¨×Ö·û£©
Ïà¹Øº¯Êý index£¬memchr£¬rindex£¬strpbrk£¬strsep£¬strspn£¬strstr£¬strtok
±íÍ·Îļþ #include
¶¨Ò庯Êý char * strrchr(const char *s, int c);
º¯Êý˵Ã÷ strrchr()ÓÃÀ´ÕÒ³ö²ÎÊýs×Ö·û´®ÖÐ×îºóÒ»¸ö³öÏֵIJÎÊýcµØÖ·£¬È»ºó½«¸Ã×Ö·û³öÏֵĵØÖ··µ»Ø¡£
·µ»ØÖµ Èç¹ûÕÒµ½Ö¸¶¨µÄ×Ö·ûÔò·µ»Ø¸Ã×Ö·ûËùÔÚµØÖ·£¬·ñÔò·µ»Ø0¡£
·¶Àý #include
main()
{
char *s="0123456789012345678901234567890";
char *p;
p=strrchr(s,'5');
printf("%s\n",p);
}
Ö´ÐÐ 567890
¡¡
strspn£¨·µ»Ø×Ö·û´®ÖÐÁ¬Ðø²»º¬Ö¸¶¨×Ö·û´®ÄÚÈݵÄ×Ö·ûÊý£©
Ïà¹Øº¯Êý strcspn£¬strchr£¬strpbrk£¬strsep£¬strstr
±íÍ·Îļþ #include
¶¨Ò庯Êý size_t strspn (const char *s,const char * accept);
º¯Êý˵Ã÷ strspn()´Ó²ÎÊýs ×Ö·û´®µÄ¿ªÍ·¼ÆËãÁ¬ÐøµÄ×Ö·û£¬¶øÕâЩ×Ö·û¶¼ÍêÈ«ÊÇaccept ËùÖ¸×Ö·û´®ÖеÄ×Ö·û¡£¼òµ¥µÄ˵£¬Èôstrspn()·µ»ØµÄÊýֵΪn£¬Ôò´ú±í×Ö·û´®s ¿ªÍ·Á¬ÐøÓÐn ¸ö×Ö·û¶¼ÊÇÊôÓÚ×Ö·û´®acceptÄÚµÄ×Ö·û¡£
·µ»ØÖµ ·µ»Ø×Ö·û´®s¿ªÍ·Á¬Ðø°üº¬×Ö·û´®acceptÄÚµÄ×Ö·ûÊýÄ¿¡£
·¶Àý #include
main()
{
char *str="Linux was first developed for 386/486-based PCs.";
char *t1="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d\n",strspn(str,t1));
}
Ö´ÐÐ 5 /*¼ÆËã´óСд×Öĸ¡£²»°üº¬¡° ¡±£¬ËùÒÔ·µ»ØLinuxµÄ³¤¶È¡£*/
¡¡
strstr£¨ÔÚÒ»×Ö·û´®ÖвéÕÒÖ¸¶¨µÄ×Ö·û´®£©
Ïà¹Øº¯Êý index£¬memchr£¬rindex£¬strchr£¬strpbrk£¬strsep£¬strspn£¬strtok
±íÍ·Îļþ #include
¶¨Ò庯Êý char *strstr(const char *haystack,const char *needle);
º¯Êý˵Ã÷ strstr()»á´Ó×Ö·û´®haystack ÖÐËÑÑ°×Ö·û´®needle£¬²¢½«µÚÒ»´Î³öÏֵĵØÖ··µ»Ø¡£
·µ»ØÖµ ·µ»ØÖ¸¶¨×Ö·û´®µÚÒ»´Î³öÏֵĵØÖ·£¬·ñÔò·µ»Ø0¡£
·¶Àý #include
main()
{
char * s="012345678901234567890123456789";
char *p;
p= strstr(s,"901");
printf("%s\n",p);
}
Ö´ÐÐ 9.01E+21
¡¡
strtok£¨·Ö¸î×Ö·û´®£©
Ïà¹Øº¯Êý index£¬memchr£¬rindex£¬strpbrk£¬strsep£¬strspn£¬strstr
±íÍ·Îļþ #include
¶¨Ò庯Êý char * strtok(char *s,const char *delim);
º¯Êý˵Ã÷ strtok()ÓÃÀ´½«×Ö·û´®·Ö¸î³ÉÒ»¸ö¸öƬ¶Î¡£²ÎÊýsÖ¸ÏòÓû·Ö¸îµÄ×Ö·û´®£¬²ÎÊýdelimÔòΪ·Ö¸î×Ö·û´®£¬µ±strtok()ÔÚ²ÎÊýsµÄ×Ö·û´®Öз¢ÏÖµ½²ÎÊýdelimµÄ·Ö¸î×Ö·ûʱÔò»á½«¸Ã×Ö·û¸ÄΪ\0 ×Ö·û¡£ÔÚµÚÒ»´Îµ÷ÓÃʱ£¬strtok()±ØÐè¸øÓè²ÎÊýs×Ö·û´®£¬ÍùºóµÄµ÷ÓÃÔò½«²ÎÊýsÉèÖóÉNULL¡£Ã¿´Îµ÷Óóɹ¦Ôò·µ»ØÏÂÒ»¸ö·Ö¸îºóµÄ×Ö·û´®Ö¸Õë¡£
·µ»ØÖµ ·µ»ØÏÂÒ»¸ö·Ö¸îºóµÄ×Ö·û´®Ö¸Õ룬Èç¹ûÒÑÎÞ´Ó·Ö¸îÔò·µ»ØNULL¡£
·¶Àý #include
main()
{
char s[]="ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z";
char *delim="-: ";
char *p;
printf("%s ";strtok(s,delim));
while((p=strtok(NULL,delim)))printf("%s ",p);
printf("\n");
}
Ö´ÐÐ ab cd ef;gh i jkl;mnop;qrs tu vwx y;z /*£Óë:×Ö·ûÒѾ±»\0 ×Ö·ûÈ¡´ú*/
suo2800 ÓÚ 2006-07-14 13:16:39·¢±í:
²»´í£¬Ñ§Ï°ÁË