c++实现字符串

#include"Head.h"
#include<assert.h>
#include<string>
char *_strcpy(char *dst, const char *src)
{
	assert(dst != NULL&&src != NULL);

	char *ret = dst;

	while ((*dst++ = *src++) != '\0');

	return ret;
}
int _mystrcmp(char *dst, const char *src)
{
	assert(dst != NULL&&src != NULL);
	while (*dst&&*src&&*dst==*src)
	{
		++dst;
		++src;
	}
	if (*dst > *src)
		return 1;
	if (*dst < *src)
		return -1;
	else
		return 0;
}
char *_strcat(char *source, int s1,const char *dest)
{
	assert(source != NULL&&dest != NULL);
	int i, j;
	i = s1;
	for (j = 0; dest[j] != 0; j++, i++)
	{
		source[i] = dest[j];
	}
	return source;
}
int _strlen(const char *_Str)
{
	assert(_Str != NULL);
	int _Size = 0;
	while (*_Str++ !='\0')
	{
		++_Size;
	}
	return _Size+1;
}
char *_strstr(char *str, char *substr)
{
	assert( str!= NULL&& substr!= NULL);
	char *pTempSrc = str;
	char *pTempsub = substr;
	do
	{
		if (*pTempSrc != *pTempsub++)
		{
			pTempsub = substr;
		}
		if (!*pTempsub)
		{
			return pTempSrc - (pTempsub - substr) + 1;
		}

	} while (*pTempSrc++);
	return "a";
}
char *_strtok(char *str, const char *delim)
{
	static char *src = NULL;
	const char *indelim = delim;
	int flag = 1, index = 0;
	char *temp = NULL;
	if (str == NULL)
	{
		str = src;
	}
	for (; *str; str++)
	{
		delim = indelim;
		for (; *delim; delim++)
		{
			if (*str == *delim)
			{
				*str = NULL;
				index = 1;
				break;
			}
				
		}
		if (*str != NULL&&flag == 1)
		{
			temp = str;
			flag = 0;
		}
		if (*str != NULL&&flag == 0 && index == 1)
		{
			src = str;
			return temp;
		}
	}
	src = str;
	return temp;
}
char *_strchr(char *str, char character)
{
	if (str == NULL)
		return NULL;
	char *p = NULL;
	while (*str!='\0')
	{
		if (*str == character)
			p = str;
		str++;
	}
	return p;
}
void main()
{
	cout << "实现了strcpy:   " << endl;
	char s[10];
	cout <<"拷贝  "<< _strcpy(s,"1234665") << endl;

	cout << "实现了mystrcmp:   " << endl;
	char l[10] = "sdfa55";
	char f[10] = "22sdfa";
	cout <<"比较l和f    "<<"结果为:  "<< _mystrcmp(l,f) << endl;

	cout << "实现了strcat:" << endl;
	char l1[13] = "sdfa55";
	char f1[10] = "22sdfa";
	cout <<"拼接  "<<_strcat(l1,6,f1) << endl;

	cout << "实现了_strlen:" << endl;
	char l2[10] = "fasdfasdd";
	cout <<"长度  " <<_strlen(l2) << endl;

	cout << "实现了strstr:    " << endl;
	char *p = _strstr("114145", "14");
	cout <<"搜寻子char:::" <<p << endl;
	cout << "实现了strtok:    " << endl;


	char s1[] = "ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z";
	char *delim = "-";
	char *p1;
	cout << _strtok(s1, delim) << endl;
	while ((p1=_strtok(NULL,delim)))
		cout << p1 << endl;

	cout << "实现了strchr:    " << endl;
	char u[] = "fiaslkal55";
	cout << _strchr(u, 'i') << endl;;

	system("pause");
	
}

 

  • 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、付费专栏及课程。

余额充值