设计一个字符串类 编写一个main()函数,测试你的字符串类的各种功能。

来源:百度知道 编辑:UC知道 时间:2024/05/31 18:46:58
设计一个字符串类,功能如下:

1) 能够用 “+” 来处理两个字符串的相加

2) 具有在一个字符串中搜索一个字符的功能

3) 具有在一个字符串中搜索另一个字符串的功能

4) 编写一个main()函数,测试你的字符串类的各种功能。

请大侠们帮帮忙啦!感激不尽!
能不能提供用C++的程序.我要的是C++!谢谢

java String类 这些方法全有,下个jdk自己看

// zd_50.cpp : Defines the entry point for the console application.
//

#include <iostream.h>
#include <string.h>
#include <stdio.h>

/*创建一个字符串类:String*/

class String{
public:
String(const char* str = NULL); //构造函数之一
String(const String& another); //构造函数之二
void input_str(); //字符串输入
String& operator +(const String& rhs); //重载加法运算符
void print(); //字符串输出
int findchar(int c);//查找字符
int findstr(const char *);//查找字符串

private:
char *m_data;
char *addstr;
};

/*构造函数之一*/

String::String(const char* str)
{
if(str == NULL) //如果输入为空字符串,则添加“\0”表示空串。
{
m_data = new char[1];
m_data[0] = '\0';
}
else //如果输入非空串,复制该字符串。
{
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}
}

/*构造函数之二*/

S