Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //#include <stdio.h>
- using namespace std;
- const int size = 100;
- class MyString{
- private:
- char str[size];
- public:
- MyString() {str[0]=0;}
- MyString(char a[]) {
- int i;
- for(i = 0; a[i]; i++)
- str[i] = a[i];
- str[i] = 0;
- }
- ~MyString() {}
- void show(){
- cout << str << endl;
- }
- MyString operator + (MyString X);
- };
- int main(){
- char a[size], b[size];
- cout << "Enter String 1: ";
- cin >> a;
- //cin.get(a, size);
- //scanf("%[^\n]s",a);
- cout << "Enter String 2: ";
- cin >> b;
- //cin.get(b, size);
- //scanf("%[^\n]s",b);
- MyString A(a), B(b);
- A.show();
- B.show();
- MyString C = A + B;
- C.show();
- return 0;
- }
- MyString MyString::operator + (MyString X){
- MyString temp;
- int i, b, x;
- for(i = 0, b = 0; str[i]; i++)
- b++;
- for(i = 0, x = 0; X.str[i]; i++)
- x++;
- if(b + x < size){
- for(i = 0; i < b; i++)
- temp.str[i] = str[i];
- for(i = 0; i < x; i++)
- temp.str[b + i] = X.str[i];
- temp.str[b + x] = 0;
- }
- else{
- cout << "Cannot Catenate" << endl;
- }
- return temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement