2. Consider the mystring class Class mystring { char str [100]; int length; Public: // methods }; Overloading the method called “check” for the class to perform the following operations. i)Check if a specific character is present in str. Returns position if found, -1 if not. ii)Compares the length of two mystring objects and returns 1 if they are equal, 0 if not. With attribute real and imaginary.


#include<iostream.h>
#include<conio.h>
class mystring
{
char str[100];
int length;
public:
void get();
int check(mystring);
int check(mystring,mystring);
};
void mystring::get()
{
cout<<“\n enter the string:”;
cin>>str;
}
int mystring::check(mystring m)
{
char c;
cout<<“\n enter the character:”;
cin>>c;
for(int i=0;m.str[i]!=”;i++)
{
if(m.str[i]==c)
{
cout<<“\n position is=”<<i+1;
}
}
return -1;
}
int mystring::check(mystring a,mystring b)
{
int j,k=0,l;
for(j=0;a.str[j]!=”;j++);
for(l=0;b.str[l]!=”;l++);
if(j==l)
{
for(int i=0;a.str[i]!=”,b.str[i]!=”;i++)
{
if(a.str[i]==b.str[i])
{
k++;
}
}
if(j==k)
{
return 1;
}
else
{
return 0;
}
}
}
void main()
{
int s;
mystring m1,m2,m3,m4;
clrscr();
m2.get();
m1.check(m2);
m3.get();
s= m4.check(m2,m3);
if(s==1)
{
cout<<“\n Both string is equal:”;
}
else
{
cout<<“\n string is not equal”;
}
getch();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s