Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5. class STRING{
  6.     public:
  7. int len;
  8. char *name;
  9.  
  10.     friend istream & operator>>(istream &,const STRING &);
  11.     friend ostream & operator<<(ostream &,const STRING &);
  12.     void ranswap(string a,string b);
  13.     STRING(){len=0; name= new char[len];}
  14.     STRING (const char *w)
  15.     {
  16.         len=strlen(w);
  17.         name= new char [len+1];
  18.         strcpy(name,w);
  19.     }
  20.     STRING(const STRING &a)
  21.     {
  22.         len=a.len;
  23.         name=new char[len+1];
  24.         strcpy(name,a.name);
  25.     }
  26.  
  27.  
  28.  
  29.  
  30.  
  31. };
  32. void STRING :: ranswap(string a,string b)
  33. {   int i,j=0;
  34.     STRING s;
  35.     len=a.size()+b.size();
  36.     name=new char [len+1];
  37.     for(i=0;i<len;i++)
  38.     {
  39.         if(i%2==0)
  40.             s[i]=a.at(j);
  41.         else
  42.             s[i]=b.at(j);
  43.     j++;
  44.     }
  45.     cout <<s;
  46. }
  47.  istream & operator>>(istream &in,const STRING &g)
  48. {  delete [] g.name;
  49.  
  50.         in>>g.name;
  51.     return (in);
  52. }
  53.  ostream & operator<<(ostream &out,const STRING &k)
  54. {
  55.  
  56.         out<<k.name;
  57.     return (out);
  58.  
  59. }
  60.  
  61. int main()
  62. {
  63.     STRING s="hello";
  64.     STRING a,n,q,h=" ";
  65.     a=s;
  66.     n="hello";
  67.     cout << n;
  68.     q="world";
  69.     h.ranswap(n,q);
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement