1、strcat
[cpp]
char *strcat(char *strDest, const char *strScr) //將源字符串加const,表明其為輸入參數
{
char * address = strDest; //該語句若放在assert之後,編譯出錯
assert((strDest != NULL) && (strScr != NULL)); //對源地址和目的地址加非0斷言
while(*strDest) //是while(*strDest!='\0')的簡化形式
{ //若使用while(*strDest++),則會出錯,因為++是不受循環
strDest++; //約束的。所以要在循環體內++;因為要是*strDest最後指
} //向該字符串的結束標志’\0’。
while(*strDest++ = *strScr++) //是while((*strDest++ = *strScr++)!='\0')的簡化形式
{
NULL; //該循環條件內可以用++,
} //此處可以加語句*strDest='\0';有無必要?
return address; //為了實現鏈式操作,將目的地址返回
}
char *strcat(char *strDest, const char *strScr) //將源字符串加const,表明其為輸入參數
{
char * address = strDest; //該語句若放在assert之後,編譯出錯
assert((strDest != NULL) && (strScr != NULL)); //對源地址和目的地址加非0斷言
while(*strDest) //是while(*strDest!='\0')的簡化形式
{ //若使用while(*strDest++),則會出錯,因為++是不受循環
strDest++; //約束的。所以要在循環體內++;因為要是*strDest最後指
} //向該字符串的結束標志’\0’。
while(*strDest++ = *strScr++) //是while((*strDest++ = *strScr++)!='\0')的簡化形式
{
NULL; //該循環條件內可以用++,
} //此處可以加語句*strDest='\0';有無必要?
return address; //為了實現鏈式操作,將目的地址返回
}
2、strcmp
[cpp]
int __cdecl strcmp (
const char * src,
const char * dst
)
{
int ret = 0;
while(!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return(ret);
}
int __cdecl strcmp (
const char * src,
const char * dst
)
{
int ret = 0;
while(!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return(ret);
}
[cpp]
int strcmp(const char *dest, const char *source)
{
assert((NULL != dest) && (NULL != source));
while (*dest && *source && (*dest == *source))
{
dest ++;
source ++;
}
return *dest - *source;
/*如果dest > source,則返回值大於0,如果dest = source,則返回值等於0,如果dest < source ,則返回值小於0。*/
}
int strcmp(const char *dest, const char *source)
{
assert((NULL != dest) && (NULL != source));
while (*dest && *source && (*dest == *source))
{
dest ++;
source ++;
}
return *dest - *source;
/*如果dest > source,則返回值大於0,如果dest = source,則返回值等於0,如果dest < source ,則返回值小於0。*/
}
3、strcpy
[cpp]
char *strcpy(char *strDestination,const char *strSource)
{
assert(strDestination!=NULL && strSource!=NULL);
char *strD=strDestination;
while ((*strD++=*strSource++)!='\0');
return strDestination;
}
char *strcpy(char *strDestination,const char *strSource)
{
assert(strDestination!=NULL && strSource!=NULL);
char *strD=strDestination;
while ((*strD++=*strSource++)!='\0');
return strDestination;
}
4、strncmp
[cpp]
char *strncpy(char *dest, const char *src, size_t count)
{
char *tmp = dest;
assert(src!=NULL && dest!=NULL);
while (count) {
if ((*tmp = *src) != 0)
src++;
tmp++;
count--;
}
return dest;
}
char *strncpy(char *dest, const char *src, size_t count)
{
char *tmp = dest;
assert(src!=NULL && dest!=NULL);
while (count) {
if ((*tmp = *src) != 0)
src++;
tmp++;
count--;
}
return dest;
}
5、strrchr
[cpp]
char *
strrchr (const char *s, int c)
{
register const char *found, *p;
c = (unsigned char) c;
/* Since strchr is fast, we use it rather than the obvious loop. */
if (c == '\0')
return strchr (s, '\0');
found = NULL;
while ((p = strchr (s, c)) != NULL)
{
found = p;
s = p + 1;
}
return (char *) found;
}
char *
strrchr (const char *s, int c)
{
register const char *found, *p;
c = (unsigned char) c;
/* Since strchr is fast, we use it rather than the obvious loop. */
if (c == '\0')
return strchr (s, '\0');
found = NULL;
while ((p = strchr (s, c)) != NULL)
{
found = p;
s = p + 1;
}
return (char *) found;
}