//to check two strings for equality
//by member function
#include <iostream>
#include <string.h>
using namespace std;
class String
{
// char *str;
char str[20];
public:
// String()
// {
// str=new char[20];
// cout<<"Memory dynamically assigned:"<<endl;
// }
void read()
{
cout<<"Enter string: ";
//cin.get(str,20,'\n');
cin>>str;
}
bool operator==(String s) //member function
{
return ((strcmp(str,s.str)==0)?true:false);
}
};
int main()
{
String s1,s2;
s1.read();
s2.read();
if(s1==s2) //same as s1.operator==(s2);
cout<<"the s1 is equal to s2.";
else
cout<<"the s1 is not equal to s2.";
return 0;
}
//by member function
#include <iostream>
#include <string.h>
using namespace std;
class String
{
// char *str;
char str[20];
public:
// String()
// {
// str=new char[20];
// cout<<"Memory dynamically assigned:"<<endl;
// }
void read()
{
cout<<"Enter string: ";
//cin.get(str,20,'\n');
cin>>str;
}
bool operator==(String s) //member function
{
return ((strcmp(str,s.str)==0)?true:false);
}
};
int main()
{
String s1,s2;
s1.read();
s2.read();
if(s1==s2) //same as s1.operator==(s2);
cout<<"the s1 is equal to s2.";
else
cout<<"the s1 is not equal to s2.";
return 0;
}
No comments:
Post a Comment