Thursday, 29 November 2012

Add two strings & compare also


/*A program to add two strings & compare also
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/

#include<iostream.h>
#include<conio.h>

class string
{
 char s[20];
 int len;
 public:
 void read()
 {
  cout<<"enter the string : ";
  cin>>s;
  len=strlen();
 }

 int strlen()
 {
  int count=0;
  for(int i=0;i<20;i++)
  {
   if(s[i]==NULL) break;
   count++;
  }
  return(count);
 }

 void display()
 {
  cout<<"String is : "<<s<<endl;
 }

 void concate(string s1,string s2)
 {
  int i,j,k;
  k=0;
  for(i=0;i<s1.len;i++,k++)
  {
   s[k]=s1.s[i];
  }
  k=s1.len;
  for(j=0;j<s2.len;j++,k++)
  {
   s[k]=s2.s[j];
  }
  len=s1.len+s2.len;
 }

 void compare(string s1)
 {
  int flag=0;
  if(len!=s1.len)
  {
   cout<<"both strings are not equal\n";
   return;
  }
  else
  {
   for(int i=0;i<len;i++)
   {
    if(s[i]!=s1.s[i])
    {
     cout<<"both the strings are not equal\n";
     flag=1;
     break;
    }
   }
   if(flag==0)
   cout<<"both the strings are equal\n";
  }
 }
};

void main()
{
 clrscr();
 string a,b,c;
 int choice;
 do
 {
  cout<<"MENU\n1.concatinate two strings\n2.compare two strings\n3.exit\n";
  cout<<"Enter your choice";
  cin>>choice;
  switch(choice)
  {
   case 1:
a.read();
b.read();
c.concate(a,b);
a.display();
b.display();
c.display() ;
break;
   case 2:
a.read();
b.read();
a.compare(b);
a.display();
b.display();
break;
  }
 }
 while(choice!=3);
}
/*=========================14April,2011=============================*/

1 comment:

You are welcome, your comment helps me to make this blog more better.