C++ string的几个函数

原创 2004年08月17日 16:26:00

int StrLen(const char* str)
{
const char *sc;

for (sc = str; *sc != '/0'; ++sc);

return sc - str;
}

char* StrTrim(char* s)
{
char* pDst = s;

while(1)
{
if (*pDst == ' ' || *pDst == '/t' || *pDst == '/n')
{
pDst++;
}
else
{
break;
}
}

char* p = pDst;
while(*p)
 {
 p++;
 }
p--;
while(1)
{
if (*p == ' ' || *p == '/t' || *p == '/n')
{
p--;
}
else
{
break;
}
}
*(p+1) = '/0';

return pDst;
}

char* StrCat(char* dst, const char* src)
{
char *pTemp = dst;

while (*pTemp)
 pTemp++;

while (*pTemp++ = *src++);

return dst;
}

int StrCmp(const char * dst, const char * src)
{
int ret = 0 ;
while (!(ret = *dst - *src) && *dst)
{
++src;
++dst;
}

if (ret < 0 )
ret = -1 ;
else if (ret > 0 )
ret = 1 ;

return ret;
}

int StrCmp(const char * dst, const char * src, int count)
{
int ret = 0 ;
while (!(ret = *dst - *src) && *dst)
{
if (--count == 0)
break;
++src;
++dst;
}

if (ret < 0 )
ret = -1 ;
else if (ret > 0 )
ret = 1 ;

return ret;
}

char* StrCpy(char * dst, const char * src)
{
char* pTemp = dst;

while (*pTemp++ = *src++);

return dst;
}

char* StrCpy(char * dst, const char * src, int count)
{
char* pTemp = dst;

while (count-- && (*pTemp++ = *src++));

return dst;
}

char * StrChr(const char * s, int c)
{
for(; *s != (char) c; ++s)
 if (*s == '/0')
 return NULL;
return (char *) s;
}

char * StrRChr(const char * s, int c)
{
 const char *p = s + StrLen(s);
 do
 {
 if (*p == (char)c)
 return (char *)p;
 } while (--p >= s);

 return NULL;
}

char * StrStr(const char * s1,const char * s2)
{
int l1, l2;

l2 = StrLen(s2);
if (!l2)
return (char *) s1;
l1 = StrLen(s1);
while (l1 >= l2) {
l1--;
if (!StrCmp(s1,s2,l2))
return (char *) s1;
s1++;
}
return NULL;
}

int StrSpn(const char *s, const char *accept)
{
const char *p;
const char *a;
int count = 0;

for (p = s; *p != '/0'; ++p)
{
for (a = accept; *a != '/0'; ++a)
{
if (*p == *a)
break;
}
if (*a == '/0')
return count;
++count;
}

return count;
}

收藏助手
不良信息举报
您举报文章:C++ string的几个函数
举报原因:
原因补充:

(最多只允许输入30个字)