Код:
#include <string>
#include <iostream>
#define MAX 6666
using namespace std;
class my_str
{
char *str;
public:
int lenth;
bool lenth_active;
int get_lenth();
static int compare(my_str &some_string1, my_str &some_string2);
int concat();
void init();
my_str();
~my_str();
};
my_str::my_str()
{
str = new char[MAX];
}
my_str::~my_str()
{
delete str;
}
int my_str::get_lenth()
{
int i;
for(i=0; ((i<MAX)&&(str[i])); i++);
lenth = i;
lenth_active = true;
return i;
}
int my_str::compare(my_str &s1, my_str &s2)
{
if( (s1.lenth_active)&&(s2.lenth_active) )
return s1.lenth==s2.lenth ? 0 : s1.lenth>s2.lenth ? 1 : -1 ;
else
{
s1.get_lenth();
s2.get_lenth();
compare(s1, s2);
}
}
void my_str::init()
{
getline(cin, str, '\n');
}
int main()
{
my_str in1, in2;
int what;
cout << "Enter the first string: ";
in1.init();
cout << "The lenth of the first string is " << in1.get_lenth() << "\n";
cout << "Enter the second string: ";
in2.init();
cout << "The lenth of the second string is " << in2.get_lenth() << "\n";
what = my_str::compare(in1, in2);
if(what == 0)
cout << "The lenths of both strings are the same.";
else
if(what == 1)
cout << "The first string is longer.";
else
if(what == -1)
cout << "The second string is longer.";
return 0;
}