/************************************************************
char* strcat ( char * dst , const char * src )
用法:#include <string.h>
功能:Concatenates src onto the end of dest. Assumes enough space in dest.
返回结果:The address of "dst"
功能:把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
***********************************************************/
char* strcat ( char * dst , const char * src )
{
assert((dst != NULL)&&(src != NULL));
char * cp = dst;
while( *cp !='\0')
cp++;
while( (*cp++ = *src++)!='\0') ;
return( dst );
}
/********************************************************************
函数名: strstr
函数原型:extern char *strstr(char *str1, char *str2);
功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。
返回值:返回该位置的指针,如找不到,返回空指针。
头文件:#include <string.h>
定义函数:char *strstr(const char *haystack, const char * needle);
函数说明:strstr()会从字符串haystack 中搜寻字符串needle, 并将第一次出现的地址返回.
返回值:返回指定字符串第一次出现的地址, 否则返回0.
The strstr() function finds the first occurrence of the substring needle
in the string haystack. The terminating '\0' characters are not compared.
********************************************************************/
char *strstr(const char *s1, const char *s2)
{
int len2;
if( !(len2 = strlen(s2)) ) //长度为0
return(char *)s1;
for( ; *s1; ++s1)
{
if(*s1 == *s2 && strncmp(s1, s2, len2) == 0 )
return (char *)s1;
}
return NULL;
}
char* myStrstr(const char* string,const char* strCharSet)
{
int i;
if(*strCharSet)
{
while(*string)
{
for(i=0; *(string+i) == *(strCharSet+i) ; i++)
{
if(!*(strCharSet+i+1))//下一个字符是'\0' 则已经查找成功
return (char*)string;
}
string++;
}
return (char*)NULL;
}
else
return (char*)string;
}
char *strstr_r(char *s1, char *s2)
{
const char *p1, *p2;
if((*s2) == '\0')
/* Early versions of Linux libc,strstr would not allow an empty needle argument */
return s1;
while( (s1 = strchr(s1, *s2)) != NULL) //查找第一个字符成功
{
p1 = s1; /* found first character of s2, see if the rest matches */
p2 = s2;
while((*p1++ == *p2++))
{
if(*p1 == '\0')
return s1; /* both strings ended together */
}
if(*p2 == '\0')
{
break; //跳出while 循环
}
s1++;
}
return s1;
}
char* strcat ( char * dst , const char * src )
用法:#include <string.h>
功能:Concatenates src onto the end of dest. Assumes enough space in dest.
返回结果:The address of "dst"
功能:把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
***********************************************************/
char* strcat ( char * dst , const char * src )
{
assert((dst != NULL)&&(src != NULL));
char * cp = dst;
while( *cp !='\0')
cp++;
while( (*cp++ = *src++)!='\0') ;
return( dst );
}
/********************************************************************
函数名: strstr
函数原型:extern char *strstr(char *str1, char *str2);
功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。
返回值:返回该位置的指针,如找不到,返回空指针。
头文件:#include <string.h>
定义函数:char *strstr(const char *haystack, const char * needle);
函数说明:strstr()会从字符串haystack 中搜寻字符串needle, 并将第一次出现的地址返回.
返回值:返回指定字符串第一次出现的地址, 否则返回0.
The strstr() function finds the first occurrence of the substring needle
in the string haystack. The terminating '\0' characters are not compared.
********************************************************************/
char *strstr(const char *s1, const char *s2)
{
int len2;
if( !(len2 = strlen(s2)) ) //长度为0
return(char *)s1;
for( ; *s1; ++s1)
{
if(*s1 == *s2 && strncmp(s1, s2, len2) == 0 )
return (char *)s1;
}
return NULL;
}
char* myStrstr(const char* string,const char* strCharSet)
{
int i;
if(*strCharSet)
{
while(*string)
{
for(i=0; *(string+i) == *(strCharSet+i) ; i++)
{
if(!*(strCharSet+i+1))//下一个字符是'\0' 则已经查找成功
return (char*)string;
}
string++;
}
return (char*)NULL;
}
else
return (char*)string;
}
char *strstr_r(char *s1, char *s2)
{
const char *p1, *p2;
if((*s2) == '\0')
/* Early versions of Linux libc,strstr would not allow an empty needle argument */
return s1;
while( (s1 = strchr(s1, *s2)) != NULL) //查找第一个字符成功
{
p1 = s1; /* found first character of s2, see if the rest matches */
p2 = s2;
while((*p1++ == *p2++))
{
if(*p1 == '\0')
return s1; /* both strings ended together */
}
if(*p2 == '\0')
{
break; //跳出while 循环
}
s1++;
}
return s1;
}