c++ 编程问题 定义一个字符串类String,有两个私有数据成员:char* content和int len;

2025-01-08 09:38:29
推荐回答(2个)
回答1:

#include using namespace std;
class String
{
private:
char *str;
int len;

public: String()
{
char *s = new char[100];
cin >> s;
str = s;
}
~String ()
{
if(str != NULL)
free(str);
}
// 得打字符串的指针
char * getStr()
{
return str;
}
// 小写变大写
char *ToBigChar()
{
char *s = str;
size_t i =0;
for( i = 0; i < strlen(str); i++)
{
if ( s[i] < 'z' || s[i] > 'a')
{
s[i] = s[i] - 32;
}
}
return str;
}
};

int main(){
String sss;
cout << sss.ToBigChar() << endl;

return 0;}

回答2:

class String
{
public:
void toUpperAndShow();
String();
void Show();
char * getString();
~String();
private:
char * content;
int len;
}
String::String()
{
char lstring[1000]={0};
int i=0,sum=0;
cout<<"Please input a string:"< cin>>lstring;
for(i=0;lstring[i]!=0;i++)
sum++;
len=sum;
content=new char[len];
for(i=0;i *(content+i)=lstring[i];
}
String::~String()
{
len=0;
delete content;
}
char * String::getString()
{
return content;
}
void String::Show()
{
cout<}
void String::toUpperAndShow()
{
int i;
for(i=0;i if(*(content+i)>='a' && *(content+i)<='z')
*(content+i)+='A'-'a';
cout<}