Friday, 22 June 2012

write a c program to overload the method called check fro the class to perform the following operation

/* consider the class mystring
class mystring
{
char str[100];
int length;
public:\\method
};
overload the method called check fro the class to perform the following
operation
i)check if a spcific character is present in str return position if found
,-1if not
ii)compare the length of two mysting object and return 1 if they are equal
, 0 if not with attribute real and imaginary*/
#include<iostream.h>
#include<string.h>
#include<conio.h>
class mystring
{
char str[100];
int len;
public:int search();
       int compare();
};
int mystring::search()
       {
       char ch;
       int pos;
       cout<<"\nenter string:-";
       cin>>str;
       len=strlen(str);
       cout<<"\nenter character which you want to search:-";
       cin>>ch;
       for(int i=0;i<len;i++)
       {
       if(str[i]==ch)
       {
       pos=i+1;
       break;
       }
       else
       pos=0;
       }
       if(pos==0)
       return -1;
       else
       return pos;
       }
int mystring::compare()
       {
       int i=0,a,b=0,c,ans;
       mystring m2;
       cout<<"\nenter first string:-";
       cin>>str;
       cout<<"\nenter second string:-";
       cin>>m2.str;
       c=strcmp(str,m2.str);
       if(c==0)
       a=1;
       else
       a=0;
       if(a==1)
       {
       ans=a+(b*i);
       return ans;
       }
       else
       {
       ans=a+(b*i);
       return ans;
       }
       }
int main()
{
mystring m1;
clrscr();
int ans=m1.search();
if(ans==-1)
cout<<"\nthe given character is not found in string";
else
cout<<"\nthe character is found at "<<ans<<" position";
ans=m1.compare();
if(ans==1)
cout<<"\nboth strings are equal";
else
cout<<"\nboth strings are not equal";
getch();
return 0;
}
/*

enter string:-peersaab                                                         
                                                                               
enter character which you want to search:-b                                    
                                                                               
the character is found at 8 position                                           
enter first string:-asd                                                        
                                                                               
enter second string:-asd                                                       

both strings are equal
*/

No comments: