Write a C++ program in which you define a class String and declare two string objects. Your class should contain special function for comparison operator == to return either the two strings are of same length or not.
#include<iostream>
#include<conio.h>
using namespace std;
class String
{
public:
char a[30],b[30];
int compare(char a[], char b[])
{
int i=0,j=0;
while(a[i]!='\0')
{
i=i+1;
}
while(b[j]!='\0')
{
j=j+1;
}
if(i==j)
{
return 1;
}
else
{
return 0;
}
}
};
int main()
{
int k;
String a1,a2;
cout<<"Enter first string ";
cin>>a1.a;
cout<<"\nEnter second string ";
cin>>a2.b;
k=a1.compare(a1.a,a2.b);
if(k==1)
{
cout<<"\nYes, the strings are of the same length";
}
else{
cout<<"\nNo, the strings are not of the same length";
}
return 0;
}
Comments
Leave a comment