Tuesday 9 December 2014

5. Write a C++ program to perform String operations : i. = Equality ii. == String Copy iii. + Concatenation iv. << To display a string v. >> To reverse a string vi. Function to determine whether a string is a palindrome To find occurrence of a sub-string. Use Operator Overloading



/* Problem Statement:

Write a C++ program to perform String operations :
i.     = Equality                 ii.     == String Copy
iii.     + Concatenation            iv.     << To display a string
v.     >> To reverse a string        vi.     Function to determine whether a string is a palindrome
vii. To find occurrence of a sub-string.
             Use Operator Overloading   */


// Code

#include<iostream>
#include<string>
#include<stdlib.h>

using namespace std;

class stg
{
  char str[20];
  int len;
  public:
  stg() { len=0; }

  void read()
  {
    cout<<"\nEnter the string : ";
    cin>>str;
  }

  int operator ~()
  {
    int i;
    len=0;
    for(i=0;str[i]!='\0';i++)
    {
     len++;
    }
    return(len);
  }

  friend istream &operator >>(istream &in,stg &s)
  {
    int i;
    ~s;
    cout<<"\nReverse string: ";
    for(i=s.len-1;i>=0;i--)
    {
      cout<<s.str[i];
    }
  }

  void operator ==(stg s)
  {
     read();
     int i;
     char str2[20];
     for(i=0;str[i]!='\0';i++)
       str2[i]=str[i];
     str2[i]='\0';
    cout<<"\nCopied String : ";
    cout<<str2;
  }

  void operator +(stg s2)
  {
    ~*this;
    int j=len;
    for(int i=0;s2.str[i]!='\0';i++)
    {
       str[j]=s2.str[i];
       j++;
    }
    str[j]='\0';
    cout<<str;
  }

  void operator =(stg s2)
  {
    int i,f=0;
    for(i=0;str[i]!='\0';i++)
    {
       if(str[i]==s2.str[i])
       {
     f=1;
       }
      else
     f=0;
     break;
    }
    if(f==1)
     cout<<"\nStings are equal!";
    else
     cout<<"\nStrings are not equal";
  }
  void operator ||(stg s2)
  {
    int i,j=0,f=0,count=0;
    cout<<"\nPositions of occurence: ";
    for(i=0;str[i]!='\0';i++)
    {
       if(str[i]==s2.str[j])
       {
    j++;
       }
       else
       {
    j=0;
       }
       if(s2.str[j]=='\0')
       {
     f=1;
     count++;
     cout<<"  "<<(i-j+2);
     j=0;
       }
    }
    cout<<"\nNo of occurence of substring: "<<count;
  }

  void operator !()
  {
    int f=0;
    ~*this;
    for(int i=0;i<len;i++)
    {
      if(str[i]==str[len-i-1])
     f=1;
      else
       {
     f=0; break;
       }
    }
    if(f==1)
      cout<<"\nIt is a pallindrome!";
    else
    cout<<"\nIt is a NOT pallindrome!";
  }


friend ostream &operator <<(ostream &out,stg &s)
  {
     out<<s.str;
     return(out);
  }
};

int main()
{
int c,l;
 char m;
 stg s1,s2;
 do
 {
 cout<<"\n\n\n\tMENU:\n1.Length of the string\n2.Copy\n3.Reverse\n4.Concatination\n5.Compare two strings\n6.Find Substring\n7.Pallindrome \n8. Exit \nChoice= ";
 cin>>c;
 switch(c)
 {
  case 1: s1.read();
      l=~s1;
      cout<<"\nLength of the string is: "<<l;
      break;
  case 2: s1==s2;
      break;
  case 3: s1.read();
      cin>>s1;
      break;
  case 4: cout<<"First String :"; s1.read();
      cout<<"Second String :"; s2.read();
      s1+s2;
      break;
  case 5: cout<<"First String :"; s1.read();
      cout<<"Second String :"; s2.read();
      s1=s2;
      break;
  case 6: cout<<"Original String :"; s1.read();
      cout<<"Substring to search :"; s2.read();
      s1||s2;
      break;
  case 7: s1.read();
      !s1;
      break;
  case 8: exit(1);
      break;
  default:cout<<"Invalid";
      break;
  }
  //cout<<"\nDo you want to continue?-y/n: ";
  //cin>>m;
  }while(c!=8);
 return 0;
}

1 comment:

  1. C++ program to Find Length of String

    Length of String is the number of character in the given String. For Example: String="Language" this string have 8 characters also this string is the size of string.

    ReplyDelete