串的基本功能实现(C++)

串第一次听说,实际上就和C++封装好的string对象类似。本例题中,利用C++自实现了string类的一些功能函数,一方面回顾c++的一些知识,另一方面也加深对string对象的了解。编程过程中,为了省事,也或多或少利用了string的函数,例如size(),clear()等。
主要函数功能介绍:

Stringv1() { length = 0; }   //构造函数
Stringv1(const Stringv1 & s);   //复制构造函数
Stringv1(const char *c);   //将c风格字符串转string对象
Stringv1 &operator=(const Stringv1 & s);   //赋值函数
void Show() const;   //展示对象信息
bool ClearString();   //清除对象
bool isEmpty() const { return (length == 0); }  //判断串是否为空
int Length() const { return length; }   //返回串的长度
string SubString(int pos, int len);   //返回从pos开始的后len长度的串
int Index(const string & s, const int pos);   //查找pos之后的第一个与s相同的子串
int Replace(const string & s1, const string & s2);   //用s2替换掉串中的s1
string StrInsert(const string & s1, int pos);   //在串的pos之后插入子串s1
//这里用到了两个友元函数,目的是为了加强练习,也可以用类的公有函数代替
friend int StrCompare(const Stringv1 & s1, const Stringv1 & s2);//比较两个串是否相同
friend Stringv1 Concat(const Stringv1 & s1, const Stringv1 & s2);//将两个串连接在一起

示例输出结果:

This is a char array!
has 21 charater.
This is a char array!
has 21 charater.
String is cleared!
The two string is equal!
Substring:
is a char
Find the first substring "is" at 2
Using "at" replace is
After 2 time replace, the string is: That at a char array!
has 21 charater.
After insert a string:
That insert at a char array!

串的基本功能实现头文件:

#pragma once
#ifndef STRINGV1_H_
#define STRINGV1_H_

#include<iostream>
#include<string>

using namespace std;

class Stringv1
{
private:
	string data;
	int length;
public:
	Stringv1() { length = 0; }
	Stringv1(const Stringv1 & s);
	Stringv1(const char *c);
	Stringv1 &operator=(const Stringv1 & s);
	void Show() const;
	bool ClearString();
	bool isEmpty() const { return (length == 0); }
	int Length() const { return length; }
	string SubString(int pos, int len);
	int Index(const string & s, const int pos);
	int Replace(const string & s1, const string & s2);
	string StrInsert(const string & s1, int pos);

	friend int StrCompare(const Stringv1 & s1, const Stringv1 & s2);
	friend Stringv1 Concat(const Stringv1 & s1, const Stringv1 & s2);
};

Stringv1::Stringv1(const Stringv1 & s)
{
	data = s.data;
	length = s.length;
}

Stringv1::Stringv1(const char *c)
{
	length = strlen(c);
	for (int i = 0; i < length; i++)
		data.push_back(c[i]);
}

Stringv1 &Stringv1::operator=(const Stringv1 & s)
{
	data = s.data;
	length = s.length;
	return *this;
}

void Stringv1::Show() const
{
	cout << data << endl;
	cout << "has " << length << " charater.\n";
}

bool Stringv1::ClearString()
{
	length = 0;
	data.clear();
	return true;
}

int StrCompare(const Stringv1 & s1, const Stringv1 & s2)
{
	if (s1.data.size() == 0 || s2.data.size() == 0) {
		cout << "Least an object is null.\n";
		exit(EXIT_FAILURE);
	}
	else {
		int i = 0;
		while (i<s1.data.size()&&i<s2.data.size()&&
				s1.data[i] == s2.data[i]) {
			i++;
		}
		if (i == s1.data.size() && i == s2.data.size())
			return 0;
		else
			return (s1.data[i] < s2.data[i]) ? -1 : 1;
	}
}

Stringv1 Concat(const Stringv1 & s1, const Stringv1 & s2)
{
	Stringv1 con;
	con.data = s1.data + s2.data;
	con.length = s1.length + s2.length;
	return con;
}

string Stringv1::SubString(int pos, int len)
{
	string out;
	if (pos + len > length) {
		cout << "The substring is too long!\n";
		exit(EXIT_FAILURE);
	}
	else
		for (int i = 0; i < len; i++)
			out.push_back(data[pos + i]);
	return out;
}

int Stringv1::Index(const string & s, const int pos)
{
	if (s.empty()) {
		cout << "is empty!\n";
		return 0;
	}

	for (int i = pos; i < length; i++) {
		int j = 0;
		while (j < s.size() && i < length&&s[j] != data[i])
			i++;
		int temp = i;
		while (j < s.size() && i < length&&s[j] == data[i]) {
			i++; j++;
		}
		if (j == s.size())
			return temp;
		else
			i = temp + 1;
	}

	return 0;
}

int Stringv1::Replace(const string & s1, const string & s2)
{
	if (s1.size() != s2.size()) {
		cout << "Must have same length!\n";
		return 0;
	}
	int time = 0;
	for (int i = 0; i < length; ) {
		int  index = Index(s1, i);
		if (index) {
			for (char temp : s2) {
				data[index] = temp;
				index++;
			}
			time++;
			i = index + 1;
		}
		else if (index == 0)
			return time;
	}
}

string Stringv1::StrInsert(const string & s1, int pos)
{
	if (s1.size() == 0)
		cout << "The string is empty!\n";
	if (pos > length)
		cout << "The index is out of range!\n";
	return this->SubString(0, pos - 1) + s1 +
		this->SubString(pos - 1, length - pos + 1);
}


#endif // !STRINGV1_H_

串的基本功能示例:

#include<iostream>
#include<string>
#include"stringv1.h"

using namespace std;

void main()
{
	char test1[] = "This is a char array!";
	Stringv1 test2(test1);
	test2.Show();
	Stringv1 test3;
	test3 = test2;
	test3.Show();
	if (test3.ClearString())
		cout << "String is cleared!\n";
	if (!StrCompare(test1, test2))
		cout << "The two string is equal!\n";
	cout << "Substring: \n";
	cout << test2.SubString(5, 10) << endl;
	string s1 = "is";
	cout << "Find the first substring \"" << s1 << "\" at ";
	cout << test2.Index(s1, 1) << endl;

	string s2 = "at";
	cout << "Using \"" << s2 << "\" replace " << s1 << endl;
	int time = test2.Replace(s1, s2);
	cout << "After " << time << " time replace, the string is: ";
	test2.Show();

	string s3 = "insert ";
	cout << "After insert a string: \n";
	cout << test2.StrInsert(s3, 6) << endl;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下载好代码后直接在linux环境下减压,make之后即可产生可执行代码,压缩文件中已经包含了可执行代码。 通过的堆分配存储结构来实现的以下功能: //生成一个值等于常量chars的string int StrAssign(HString *str,char *chars); //返回string的长度 int StrLength(HString str); //比较两个的大小,如果str1 > str2,返回值>0,如果相等,返回0,如果str1 < str2,返回值<0 int StrCompare(HString str1,HString str2); //清空,释放所占用的空间 int ClearString(HString *str); //返回Str1和Str2联合而成的 HString Concat(HString str1,HString str2); //返回str的第pos个字符之后的长度为len的子 HString SubString(HString str,int pos,int len); //显示字符串 int StrTrave(HString str); //-----------------------附加操作函数-------------------------- //以下操作可由基本操作来实现 //str1复制得到str2 int StrCopy(HString str1,HString *str2); //str为空,返回1,否则返回0 int StrEmpty(HString str); //如果主str中存在和substr相等的子,则返回子在主中pos个字符之后第一次出现的位置 ,运用了KMP算法 int Index(HString str,HString substr,int pos); //Index中包括了一个静态函数get_next(),这个函数可以得到字符串的最简匹配值(kmp算法中字符匹配失败后的下一个最佳匹配值) //用字符串Tstr替换主str中出现的所有与substr相等的子 int StrReplace(HString **str,HString substr,HString Tstr); //在str的第pos个字符之后插入substr int StrInsert(HString *str,HString substr,int pos); //从str的第pos个字符起删除len个字符 int StrDelete(HString **str,int pos,int len); //销毁现有str int StrDestory(HString *str);

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值