这题帮忙下,C++高手来

来源:百度知道 编辑:UC知道 时间:2024/06/25 00:12:19
简述String类中Setc、Getc和Append三个函数的功能。
#include<iostream.h>
#include<string.h>
class String
{
public:
String(){Length=0;Buffer=0;}
String(const char* str);
void Setc(int index,char newchar);
char Getc(int index) ;
int GetLength() {return Length;}
void Print()
{
if(Buffer==0)
cout<<"empty.\n";
else
cout<<Buffer<<endl;
}
void Append(const char* Tail);
~String(){delete[] Buffer;}
private:
int Length;
char* Buffer;
};
String::String(const char*str)
{
Length=strlen(str);
Buffer=new char[Length+1];
strcpy(Buffer,str);
}
void String::Setc(int index,char newchar)
{
if(index>0&&index<=Length)
Buffer[index-1]=newchar;
}
char String::Getc(int index)
{
if(index>0&&index<=Length)
return Buffer[index

Setc:将newchar的值赋给字符串的第index位,
Getc:返回字符串中的第index位的字符,
Append:向字符串尾部追加一个字符串,

举例如下:
String("abcdef");
String.Setc(3,'z'); //字符串变为"abzdef"
String.Print();
char cTempChar = String.Getc(4); //取回了第4位的字符'd'
String.Print();
std::cout << cTempChar;
String.Append("ijk"); //追加到字符串尾部"abzdefijk"
String.Print();

上机试一下吧,有不懂的再问~~

Setc 设置string串中 第index 个值
Getc 取得string串中 第index 个值,返回其值
Append 在string 后面 加入 字符串。。