Advertisement
skb50bd

MyString w/o #include<string>

Jun 23rd, 2015
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. //#include <stdio.h>
  3. using namespace std;
  4.  
  5. const int size = 100;
  6.  
  7. class MyString{
  8. private:
  9.     char str[size];
  10. public:
  11.     MyString() {str[0]=0;}
  12.  
  13.     MyString(char a[]) {
  14.         int i;
  15.         for(i = 0; a[i]; i++)
  16.             str[i] = a[i];
  17.         str[i] = 0;
  18.     }
  19.  
  20.     ~MyString() {}
  21.  
  22.     void show(){
  23.         cout << str << endl;
  24.     }
  25.  
  26.     MyString operator + (MyString X);
  27. };
  28.  
  29.  
  30. int main(){
  31.  
  32.     char a[size], b[size];
  33.  
  34.     cout << "Enter String 1: ";
  35.     cin >> a;
  36.     //cin.get(a, size);
  37.     //scanf("%[^\n]s",a);
  38.     cout << "Enter String 2: ";
  39.     cin >> b;
  40.     //cin.get(b, size);
  41.     //scanf("%[^\n]s",b);
  42.  
  43.     MyString A(a), B(b);
  44.  
  45.     A.show();
  46.     B.show();
  47.  
  48.     MyString C = A + B;
  49.  
  50.     C.show();
  51.  
  52.     return 0;
  53. }
  54.  
  55.  
  56. MyString MyString::operator + (MyString X){
  57.     MyString temp;
  58.     int i, b, x;
  59.  
  60.     for(i = 0, b = 0; str[i]; i++)
  61.         b++;
  62.     for(i = 0, x = 0; X.str[i]; i++)
  63.         x++;
  64.  
  65.     if(b + x < size){
  66.         for(i = 0; i < b; i++)
  67.             temp.str[i] = str[i];
  68.         for(i = 0; i < x; i++)
  69.             temp.str[b + i] = X.str[i];
  70.         temp.str[b + x] = 0;
  71.     }
  72.  
  73.     else{
  74.         cout << "Cannot Catenate" << endl;
  75.     }
  76.     return temp;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement