C字符串操作函数实现
时间:2010-06-20 来源:violetjiao
#ifndef NULL
#define NULL ((void *)0)
#endif
/*
memcpy的实现代码
*/
void* memcpy(void *pDst, void *pSrc, int iLen)
{
char *pTmp = (char *)pDst;
char *pTmp2 = (char *)pSrc;
if(0 == iLen)
return pDst;
while(iLen--)
*pTmp++ = *pTmp2++;
return pDst;
}
/*
memset的实现代码
*/
void* memset(void *pDst, int iSet, int iLen)
{
char *pTmp = (char *)pDst;
if(0 == iLen)
return pDst;
while(iLen--)
*pTmp++ = (char)iSet;
return pDst;
}
/*
strcpy的实现代码
*/
char *strcpy(char *pDst, char *pSrc)
{
char *pRst = pDst;
do
*pDst++ = *pSrc;
while(*pSrc++);
return pRst;
}
/*
strcat的实现代码
*/
char *strcat(char *s, char *a)
{
char *save = s;
for(; *s; ++s);
while((*s++ = *a++) != 0);
return save;
}
/*
strlen的实现代码
*/
int strlen(char *pStr)
{
int iLen = 0;
while(*pStr++)
iLen++;
return iLen;
}
/*
strcmp的实现
*/
int strcmp(char *s, char *t)
{
for(; *s == *t; s++, t++)
{
if(('\0' == *s) || ('\0' == *t))
{
if(*s == *t)
return 0;
else
break;
}
}
return ((*s > *t) ? 1 : -1);
}
/*
strncmp的实现
*/
int m_strncmp(char *s, char *t, int n)
{
if(0 == n)
return 0;
for (; (--n > 0) && (*s==*t); s++,t++)
{
if ('\0'==*s)
return 0;
}
if(*s == *t)
return 0;
return ((*s > *t) ? 1 : -1);
}
/*
strstr的实现
*/
char* strstr(char *s, char *find)
{
char c, sc;
unsigned int len;
if ((c = *find++) != 0)
{
len = lzs_strlen(find);
do
{
do
{
if ((sc = *s++) == 0)
return (NULL);
} while (sc != c);
} while (lzs_strncmp(s, find, len) != 0);
s--;
}
return ((char *)s);
}
#define NULL ((void *)0)
#endif
/*
memcpy的实现代码
*/
void* memcpy(void *pDst, void *pSrc, int iLen)
{
char *pTmp = (char *)pDst;
char *pTmp2 = (char *)pSrc;
if(0 == iLen)
return pDst;
while(iLen--)
*pTmp++ = *pTmp2++;
return pDst;
}
/*
memset的实现代码
*/
void* memset(void *pDst, int iSet, int iLen)
{
char *pTmp = (char *)pDst;
if(0 == iLen)
return pDst;
while(iLen--)
*pTmp++ = (char)iSet;
return pDst;
}
/*
strcpy的实现代码
*/
char *strcpy(char *pDst, char *pSrc)
{
char *pRst = pDst;
do
*pDst++ = *pSrc;
while(*pSrc++);
return pRst;
}
/*
strcat的实现代码
*/
char *strcat(char *s, char *a)
{
char *save = s;
for(; *s; ++s);
while((*s++ = *a++) != 0);
return save;
}
/*
strlen的实现代码
*/
int strlen(char *pStr)
{
int iLen = 0;
while(*pStr++)
iLen++;
return iLen;
}
/*
strcmp的实现
*/
int strcmp(char *s, char *t)
{
for(; *s == *t; s++, t++)
{
if(('\0' == *s) || ('\0' == *t))
{
if(*s == *t)
return 0;
else
break;
}
}
return ((*s > *t) ? 1 : -1);
}
/*
strncmp的实现
*/
int m_strncmp(char *s, char *t, int n)
{
if(0 == n)
return 0;
for (; (--n > 0) && (*s==*t); s++,t++)
{
if ('\0'==*s)
return 0;
}
if(*s == *t)
return 0;
return ((*s > *t) ? 1 : -1);
}
/*
strstr的实现
*/
char* strstr(char *s, char *find)
{
char c, sc;
unsigned int len;
if ((c = *find++) != 0)
{
len = lzs_strlen(find);
do
{
do
{
if ((sc = *s++) == 0)
return (NULL);
} while (sc != c);
} while (lzs_strncmp(s, find, len) != 0);
s--;
}
return ((char *)s);
}
相关阅读 更多 +