Advertisement
aurko96

class_string

May 6th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class myString{
  4.     int *intAr;
  5.     int size;
  6.     int index;
  7. public:
  8.     myString()
  9.     {
  10.         intAr=new int[10];
  11.         size=10;
  12.         index=0;
  13.     }
  14.     myString(int x)
  15.     {
  16.         intAr=new int[x];
  17.         size=x;
  18.         index=0;
  19.     }
  20.     myString(int x,int ara[100])
  21.     {
  22.         intAr=new int[x];
  23.         size=x;
  24.         for(int i=0;i<size;i++) intAr[i]=ara[i];
  25.         index=size;
  26.     }
  27.     ~myString()
  28.     {
  29.         cout<<"Deleting: "<<intAr<<endl;
  30.         delete intAr;
  31.     }
  32.     void set_data(int x)
  33.     {
  34.        if(index==size) cout<<"Memory Full at size "<<size+1<<endl;
  35.        else if(x<0 || x>255) cout<<"Invalid Input\n";
  36.        else
  37.        {
  38.            intAr[index]=x;
  39.            index++;
  40.        }
  41.     }
  42.     void print_data()
  43.     {
  44.         for(int i=0;i<index;i++) cout<<(char)intAr[i]<<" ";
  45.     }
  46.     void edit_data(int i,int x)
  47.     {
  48.         if((i-1)==size) cout<<"Index out of bounds\n";
  49.         else
  50.         {
  51.             intAr[i-1]=x;
  52.         }
  53.     }
  54.     void concatenate(myString *abc)
  55.     {
  56.         int sz,k=0;
  57.         if(index==0) sz=abc->index;
  58.         else sz=index+1+abc->index;
  59.         intAr=(int *) realloc(intAr,sizeof(int)*sz);
  60.         for(int i=index;i<sz;i++)
  61.         {
  62.             intAr[i]=abc->intAr[k++];
  63.         }
  64.         size=sz;
  65.         index=sz-1;
  66.     }
  67.     void _reverse()
  68.     {
  69.         reverse(intAr,intAr+index);
  70.     }
  71. };
  72. int main()
  73. {
  74.     int n,tot;
  75.     myString str3;
  76.     cout<<"Give the number of values to be scanned: ";
  77.     cin>>tot;
  78.     cout<<"Object with default constructor:\n";
  79.     cout<<"Give ASCII value of the characters as inputs:\n";
  80.     for(int i=0;i<tot;i++)
  81.     {
  82.         cin>>n;
  83.         str3.set_data(n);
  84.     }
  85.     str3.print_data();
  86.     cout<<endl;
  87.     cout<<"Edit element of that object:\n";
  88.     int p,q;
  89.     cout<<"Input index number of the element you want to edit: ";
  90.     cin>>p;
  91.     cout<<"Input ASCII value of the new character: ";
  92.     cin>>q;
  93.     str3.edit_data(p,q);
  94.     str3.print_data();
  95.     cout<<endl;
  96.     cout<<"Object with constructor with parameter as size:\n";
  97.     int x;
  98.     cout<<"Input size of values to be scanned:\n";
  99.     cin>>x;
  100.     myString str(x);
  101.     cout<<"Give ASCII values as inputs:\n";
  102.     for(int i=0;i<x;i++)
  103.     {
  104.         cin>>n;
  105.         str.set_data(n);
  106.     }
  107.     str.print_data();
  108.     cout<<endl<<endl;
  109.     int ara[]={65,66,67,68,69};
  110.     myString str2(5,ara);
  111.     cout<<"Object with constructor parameter with a size and a given array:\n";
  112.     str2.print_data();
  113.     cout<<endl;
  114.     cout<<"Concatenating:\n";
  115.     str.concatenate(&str2);
  116.     str.print_data();
  117.     cout<<endl;
  118.     cout<<"Reversing:\n";
  119.     str._reverse();
  120.     str.print_data();
  121.     cout<<endl;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement