当前位置: 首页 > 工具软件 > vstring > 使用案例 >

自己编写的MyString实现c++中VString的一些功能

梁新觉
2023-12-01

自己实现了VString中的一些功能,用以更好地理解字符串操作。希望可以互相交流。

#include <iostream>
#include <malloc.h>
#include <stdarg.h>

class MyString {
public:
	MyString(char* s) 
	{
		len = getLength(s)+1;
		str = getOneMallocString(s, len);
	}

	MyString()
	{

	}

	void destory()
	{
		free(str);
	}

	void setText(char* s)
	{
		if (str != NULL)
			free(str);
		len = getLength(s) + 1;
		str = getOneMallocString(s, len);
	}

	char* getText()
	{
		return str;
	}

	int getLength()
	{
		int num = 0;
		while (*(str+num) != '\0')
		{
			num++;
		}
		return num;
	}

	int getLength(char* s)
	{
		int num = 0;
		while (*(s + num) != '\0') {
			num++;
		}
		return num;
	}

	int getLength(const char* s)
	{
		int num = 0;
		while (*(s + num) != '\0') {
			num++;
		}
		return num;
	}

	bool isEmpty()
	{
		if (getLength() == 0)
			return true;
		else
			return false;
	}

	MyString left(int nCount)
	{
		MyString m = MyString();
		
		char* s = (char*)malloc((nCount + 1) * sizeof(char));
		int num = 0;
		while ( *(str + num) != '\0' && (num < nCount))
		{
			*(s + num) = *(str + num);
			num++;
		}
		*(s + num) = '\0';
		
		m.setText(s);
		free(s);

		return m;
	}

	/*loadString函数待开发*/

	/*大写转变为小写*/
	void makeLower()
	{
		int num = 0;
		while (*(str + num) != '\0')
		{
			if (*(str + num) >= 'A' && *(str + num) <= 'Z')
				*(str + num) += 32;
			num++;
		}
	}

	/*小写转变为大写*/
	void makeUpper()
	{
		int num = 0;
		while (*(str + num) != '\0')
		{
			if (*(str + num) >= 'a' && *(str + num) <= 'z')
				*(str + num) -= 32;
			num++;
		}
	}

	/*字符倒置*/
	void makeReverse()
	{
		int length = 0, i;
		char temp;

		length = getLength();

		for (i = 0; i < length/2; i++) {
			temp = *(str + i);
			*(str + i) = *(str + length - 1 - i);
			*(str + length - 1 - i) = temp;
		}
	}


	/*获得字符串中几个连续字符*/
	MyString mid(int nFirst, int nCount)
	{
		MyString m = MyString();
		char* s = (char*)malloc((nCount+1) * sizeof(char));
		int i;

		for (i = nFirst; i < nFirst + nCount; i++) {
			*(s + i - nFirst) = *(str + i);
		}
		*(s + i - nFirst) = '\0';

		m.setText(s);
		free(s);

		return m;
	}

	/*删除字符串中的特定字符*/
	int remove(char c)
	{
		int result = 0,num = 0;
		
		while (*(str+num)!='\0')
		{
			if (*(str + num) == c) {
				for (int j = num;j < getLength();j++) {
					*(str + j) = *(str + j + 1);
				}
				result++;
				continue;		/*防止连续出现需要被删除的字符,跳过num++环节,重新检查该位置*/
			}

			num++;
		}
		return result;
	}


	/*替换单个字符*/
	int replace(char oldChar, char newChar)
	{
		int result = 0;

		for (int i = 0;i < getLength();i++) {
			if (*(str + i) == oldChar) {
				*(str + i) = newChar;
				result++;
			}
		}

		return result;
	}

	/*替换多个字符*/
	int replace(char* oldS, char* newS)
	{
		int result = 0, oldLength = 0, newLength = 0;
		int i = 0, j;
		bool flag = true;
		char *oldString = NULL, *newString = NULL;

		/*首先获得oldString长度*/
		oldLength = getLength(oldS);
		oldString = getOneMallocString(oldS, oldLength + 1);
		/*获得newString长度*/
		if (newS == NULL){
			newLength = 1;
			newString = (char*)malloc((newLength + 1) * sizeof(char));
			*newString = ' ';
			*(newString + 1) = '\0';
		} else {
			newLength = getLength(newS);
			newString = getOneMallocString(newS, newLength + 1);
		}
		
		/*遍历整个字符串*/
		while(*(str + i) != '\0') {
			/*每次都更新flag*/
			flag = true;
			if (*(str + i) == *oldString) {
				/*从之后开始对比每一个字符,第一个字符已经在上层循环对比过了*/
				for (j = i + 1;j < oldLength+i;j++) {
					/*找到一个不匹配的字符,则跳出循环*/
					if (*(str + j) != *(oldString + j - i)) {
						flag = false;
						break;
					}
				}
				/*如果找到了字符,则开始进行替换,字符头位置为i*/
				if (flag) {
					/*先删除*/
					deleteCharInString(i, oldLength, str);
					/*这里需要重新分配空间*/
					if (len < (getLength() + 1 + newLength)) {
						len = getLength() + 1 + newLength;
						char* temp = getOneMallocString(str, len);
						free(str);
						str = temp;
					}
					/*再腾出空间*/
					for (j = getLength();j >= i; j--) {
						*(str + j + newLength) = *(str + j);
					}
					/*再进行替换*/
					for (j = i;j < newLength + i;j++) {
						*(str + j) = *(newString + j - i);
					}
					result++;
					/*防止新字符串中含有老字符串,跳过对新插入字符串的检查*/
					i = i + newLength;
				} else {
					i++;
				}
			} else {
				i++;	/*移动指针,防止死在循环中*/
			}
			
		}

		free(oldString);
		free(newString);

		return result;
	}

	/*返回字符串中匹配的最后一个字符的索引*/
	int reverseFind(char c) 
	{
		int index = -1, i = 0;
		while ( *(str + i) != '\0')
		{
			if (*(str + i) == c) {
				index = i;
			}
			i++;
		}
		return index;
	}

	int find(char target)
	{
		int index = -1;

		for (int i = 0;i < getLength();i++) {
			if (*(str + i) == target) {
				index = i;
				break;
			}
		}
		return index;
	}

	int find(char* target)	/*在字符串s中查找字符串target第一次出现的地方*/
	{
		int index = -1;			
		int i, j, sLength, tLength;
		bool flag = true;

		sLength = getLength();
		tLength = getLength(target);
		
		for (i = 0;i < sLength;i++) {
			flag = true;
			if (*(str + i) == *target) {
				for (j = i + 1;j < i + tLength;j++) {
					if (*(str + j) != *(target + j - i)) {
						flag = false;
						break;
					}
				}

				if (flag) {
					index = i;
				}
			}
		}

		return index;
	}

	int find(char target, int start)
	{
		int index = -1;
		for (int i = start;i < getLength();i++) {
			if (*(str + i) == target) {
				index = i;
				break;
			}
		}

		return index;
	}

	int find(char* target, int start)
	{
		int index = -1;
		int i, j, sLength, tLength;
		bool flag = true;

		sLength = getLength();
		tLength = getLength(target);

		for (i = start;i < sLength;i++) {
			flag = true;
			if (*(str + i) == *target) {
				for (j = i + 1;j < i + tLength;j++) {
					if (*(str + j) != *(target + j - i)) {
						flag = false;
						break;
					}
				}

				if (flag) {
					index = i;
				}
			}
		}

		return index;
	}



	/*返回字符串末尾的ncount个字符*/
	MyString right(int nCount)
	{
		MyString m = MyString();
		char* s = (char*)malloc((nCount + 1) * sizeof(char));
		int i;
		
		for (i = 0;i < nCount;i++) {
			*(s + i) = *(str + getLength() - nCount + i);
		}
		*(s + i) = '\0';
		m.setText(s);

		free(s);

		return m;
	}

	/*把索引为index的字符替换为c*/
	void setAt(int index,char c)
	{
		*(str + index) = c;
	}

	/*删除转义字符及空格,直到遇到第一个不是这些的字符*/
	void trimLeft()
	{
		int i = 0;

		if(*(str + i) != '\0') {
			while (*(str + i) == ' ' || *(str + i) == '\t' || *(str + i) == '\n' || *(str + i) == '\r') {
				deleteCharInString(i, 1, str);
			}
		}
	}

    void formate(const char* formate, ...)
	{	
		va_list ap;
		va_start(ap, formate);
		int count = vsnprintf(str, getLength() * sizeof(char), formate, ap);		/*返回写入字符个数(不包含终止符)*/
		va_end(ap);

		if (getLength() < count) {
			int size = count * sizeof(char);
			char* temp = (char*)malloc(size);

			va_start(ap, formate);
			vsnprintf(temp, size, formate, ap);
			va_end(ap);

			free(str);
			str = temp;
		}
	}

	/*比较字符串的大小*/
	int compare(char* str2)
	{
		int result = 0;
		if (getLength() > getLength(str2))
			result = 1;
		else if (getLength() < getLength(str2))
			result = -1;
		else {
			for (int i = 0;i < getLength();i++) {
				if (*(str + i) > *(str2 + i)) {
					result = 1;
					break;
				}
				else if (*(str + i) < *(str2 + i)) {
					result = -1;
					break;
				}
			}
		}

		return result;
	}

	int Delete(int index, int count)
	{
		int oldLength = getLength();
		if (count <= oldLength) {
			deleteCharInString(index, count, str);
		}
		return oldLength;
	}

	int insert(int index, char c)
	{
		int newLength = getLength();
		if (index < getLength())
			newLength = getLength() + 1;
		int i;
		char* temp = getOneMallocString(str, newLength+1);
		for (i = getLength(temp);i >= index; i--) {
			*(temp + i + 1) = *(temp + i);
		}
		*(temp + index) = c;

		free(str);
		str = temp;

		return newLength;
	}

	int insert(int index, char* s)
	{		
		int newLength = getLength();
		if (index < getLength())
			index = getLength() + getLength(s);
		char* str2 = getOneMallocString(s, getLength(s) + 1);
		char* temp = getOneMallocString(str, newLength + 1);
		int i;

		for (i = getLength(temp);i >= index;i--) {
			*(temp + i + getLength(s)) = *(temp + i);
		}

		for (i = index;i < getLength(s) + index;i++) {
			*(temp + i) = *(str2 + i - index);
		}

		free(str);
		str = temp;
		free(str2);
		return newLength;
	}

private:
	char* str = NULL;
	int len = -1;

	/*len为包括'\0'的长度*/
	char* getOneMallocString(char* s, int len)
	{
		char* temp = (char*)malloc(len * sizeof(char));
		for (int i = 0;i < len;i++) {
			*(temp + i) = *(s + i);
		}
		return temp;
	}

	void deleteCharInString(int nIndex,int nCount, char* s)
	{
		for (int i = nIndex;i < getLength(s) - nCount + 1;i++) {
			*(s + i) = *(s + i + nCount);
		}
	}
};



int main()
{	
	
	//实现字符串的连接??
	int b = 10;
	int gaowei = b / 10;
	int diwei = b % 10;

	char a[] = { (char)(diwei  + 48) ,0};
	char newString[] = "aaaa";
	char oldString[] = "%d";
	MyString s = MyString(a);


	

	std::cout << s.getText();
	std::cout << "\n";
	std::cout << s.insert(2,newString);
	//std::cout << s.find(oldString);
	//s.formate("%cbbbbbbbbbbbbdjaldkjfalkdjf",'a');
	
	std::cout << "\n";
	std::cout << s.getText();
	s.destory();
}
 类似资料: