字符串类函数的实现

char *strcpy(char *strDest ,const char * strSrc )

{

if(strDest == NULL || strSrc == NULL)

return 0;

char *address = strSrc;

while((*strDest++ = *strSrc++) != '\0')

NULL;

return addressl;

}

//字符串比较函数,相等返回0,s1大返回正整数,s2大返回负整数
int mystrcmp(const char *s1,const char *s2)
{
    while(*s1!='/0'&&*s2!='/0'&&(*s1==*s2))  s1++,s2++;
    return (*s1)-(*s2);
}

//字符串长度函数,长度不包括结尾的'/0'
int mystrlen(const char *s)
{
int res = 0;
while(*s++!='/0')
   res++;
return res;
}

//字符串连接函数
char *mystrcat(char *source,const char *dest)
{

if(source ==NULL || dest == NULL)

return 0;
char *s = source;
while(*s!='/0')s++;
while(*dest!='/0')*s++ = *dest++;
*s='/0';
return source;
}

//字符串转换小写字母函数
char *mystrlwr(char *source)
{
   char *s = source;
   do 
   {
   if(*s>='A'&&*s<='Z')
      *s+=32;
   } while (*s++!='/0');
   return source;
}
//字符串转换大写字母函数
char *mystrupr(char *source)
{
char *s = source;
do 
{
   if(*s>='a'&&*s<='z')
    *s-=32;
} while (*s++!='/0');
    return source;
}

/字符串倒转函数
char *mystrrev(char *source)
{
char *s1 = source,*s2 = source,temp;
while(*s2!='/0')s2++;
if(s1!=s2)s2--;//找中值,交换分界点
while(s1<s2)temp = *s1,*s1 = *s2,*s2 = temp,s1++,s2--;
return source;
}

//字符串查找指定字符串的第一次出现,没有出现返回NULL,出现则返回出现的位置
static const char* _strstr(const char* s1, const char* s2)
{
assert(s2 && s1);
const char* p=s1, *r=s2;
while(*p!="\0”)
{
while(*p++==*r++);
if(*r=="\0”)
return p;
else
{
r=s2;
p=++s1;
}
}
return NULL;
}

  1. //strSrc指向的字符串循环右移n个字符后保存到strDest中并返回
  2. char * circulate_right_move(char *strDest,const char *strSrc,const int n)
  3. {
  4.     assert( (strDest != NULL) && (strSrc != NULL));
  5.     
  6.     char * address = strDest;
  7.     int len = strlen(strSrc);
  8.     int offset = n % len;
  9.     
  10.     strSrc += (len - offset);
  11.     
  12.     for(;(*strDest = *strSrc) != '\0';strDest++,strSrc++);
  13.     
  14.     strSrc -= len;    
  15.     
  16.     for(int i = 0; i < len - offset; i++)
  17.     {
  18.         *strDest = *strSrc;
  19.         strDest++;
  20.         strSrc++;
  21.     }
  22.     
  23.     *strDest = '\0';

  24.     return address;
  25. }
感觉可以用CMP算法优化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己实现字符串类 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值