Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <cstring>
- #include <cctype>
- using namespace std;
- class str_type {
- char string[80];
- public:
- str_type(char *str = "") { strcpy(string, str); }
- str_type operator+(str_type op_r);
- str_type operator+(char *str);
- str_type operator=(str_type op_r);
- str_type operator=(char *str);
- str_type operator-(str_type op_r);
- void show() { cout << string; }
- friend str_type operator+(char *str, str_type obj);
- };
- str_type str_type::operator+(str_type op_r)
- {
- str_type temp;
- strcpy(temp.string, string);
- strcat(temp.string, op_r.string);
- return temp;
- }
- str_type str_type::operator+(char *str)
- {
- str_type temp;
- strcpy(temp.string, string);
- strcat(temp.string, str);
- return temp;
- }
- str_type str_type::operator=(str_type op_r)
- {
- strcpy(string, op_r.string);
- return *this;
- }
- str_type str_type::operator=(char *str)
- {
- strcpy(string, str);
- return *this;
- }
- str_type str_type::operator-(str_type op_r)
- {
- str_type temp;
- char *s_ptr = string;
- char BUF[80];
- int buf_i = 0;
- while (*s_ptr != '\0') // It runs until the end of LEFT OBJECT string
- {
- while (isalpha(*s_ptr)) // Scaning one word
- {
- BUF[buf_i] = *s_ptr;
- buf_i++;
- s_ptr++;
- }
- if (buf_i > 0) // If word
- BUF[buf_i] = '\0';
- else { // If symbol
- BUF[buf_i] = *s_ptr;
- BUF[buf_i + 1] = '\0';
- }
- if (strcmp(op_r.string, BUF)) // Compare word with RIGHT OBJECT string
- strcat(temp.string, BUF);
- if (buf_i == 0) s_ptr++; // Next pointer position if current position is symbol
- buf_i = 0; // Reset buffer for writing
- }
- return temp;
- }
- str_type operator+(char *str, str_type obj)
- {
- str_type temp;
- strcat(temp.string, str);
- strcat(temp.string, obj.string);
- return temp;
- }
- int main()
- {
- str_type ob1("It is very-very hard test"), ob2("very"), ob_res;
- ob_res = ob1 - ob2;
- cout << "Source: "; ob1.show();
- cout << "\nDelete from source: "; ob2.show();
- cout << "\nResult: "; ob_res.show();
- cout << "\n\n";
- str_type newob("Added: ");
- newob = newob + "First";
- newob.show();
- cout << "\n\n";
- newob = "Also " + newob;
- newob.show();
- cout << "\n";
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement