Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include "string"
- #include <new>
- #include <cstring>
- #include <cstdlib>
- using namespace std;
- class StrType {
- int size;
- public:
- char *p;
- StrType();
- StrType(char *str);
- StrType(const StrType &o); // copy constructor
- ~StrType() { delete[] p; }
- friend ostream &operator<<(ostream &stream, StrType &o);
- friend istream &operator>>(istream &stream, StrType &o);
- StrType operator=(StrType &o); // assign a StrType object
- StrType operator=(char *s);
- StrType operator=(const char *s);// assign a quoted string
- StrType operator+(StrType &o); // concatenate a StrType object
- StrType operator+(char *s); // concatenate a quoted string
- friend StrType operator+(char *s, StrType &o); /* concatenate
- a quoted string with a StrType object */
- StrType operator-(StrType &o); // subtract a substring
- StrType operator-(char *s); // subtract a quoted substring
- void operator+=(StrType &o);
- void operator+=(char *s);
- bool insertstr(StrType &p, StrType &o, int position);
- // relational operations between StrType objects
- int operator==(StrType &o) { return !strcmp(p, o.p); }
- int operator!=(StrType &o) { return strcmp(p, o.p); }
- int operator<(StrType &o) { return strcmp(p, o.p) < 0; }
- int operator>(StrType &o) { return strcmp(p, o.p) > 0; }
- int operator<=(StrType &o) { return strcmp(p, o.p) <= 0; }
- int operator>=(StrType &o) { return strcmp(p, o.p) >= 0; }
- // operations between StrType objects and quoted strings
- int operator==(char *s) { return !strcmp(p, s); }
- int operator!=(char *s) { return strcmp(p, s); }
- int operator<(char *s) { return strcmp(p, s) < 0; }
- int operator>(char *s) { return strcmp(p, s) > 0; }
- int operator<=(char *s) { return strcmp(p, s) <= 0; }
- int operator>=(char *s) { return strcmp(p, s) >= 0; }
- int strsize() { return strlen(p); } // return size of string
- void makestr(char *s) { strcpy(s, p); } // null-terminated string
- operator char *() { return p; } // conversion to char *
- };
- // No explicit initialization.
- StrType::StrType() {
- size = 1; // make room for null terminator
- try {
- p = new char[size];
- }
- catch (bad_alloc xa) {
- cout << "Allocation error\n";
- exit(1);
- }
- strcpy(p, "");
- }
- // Initialize using a quoted string.
- StrType::StrType(char *str) {
- size = strlen(str) + 1; // make room for null terminator
- try {
- p = new char[size];
- }
- catch (bad_alloc xa) {
- cout << "Allocation error\n";
- exit(1);
- }
- strcpy(p, str);
- }
- // Initialize using a StrType object.
- StrType::StrType(const StrType &o) {
- size = o.size;
- try {
- p = new char[size];
- }
- catch (bad_alloc xa) {
- cout << "Allocation error\n";
- exit(1);
- }
- strcpy(p, o.p);
- }
- // Output a string.
- ostream &operator<<(ostream &stream, StrType &o)
- {
- stream << o.p;
- return stream;
- }
- // Input a string.
- istream &operator>>(istream &stream, StrType &o)
- {
- char t[255]; // arbitrary size - change if necessary
- int len;
- stream.getline(t, 255);
- len = strlen(t) + 1;
- if (len > o.size) {
- delete[] o.p;
- try {
- o.p = new char[len];
- }
- catch (bad_alloc xa) {
- cout << "Allocation error\n";
- exit(1);
- }
- o.size = len;
- }
- strcpy(o.p, t);
- return stream;
- }
- // Assign a StrType object to a StrType object.
- StrType StrType::operator=(StrType &o)
- {
- StrType temp(o.p);
- if (o.size > size) {
- delete[] p; // free old memory
- try {
- p = new char[o.size];
- }
- catch (bad_alloc xa) {
- cout << "Allocation error\n";
- exit(1);
- }
- size = o.size;
- }
- strcpy(p, o.p);
- strcpy(temp.p, o.p);
- return temp;
- }
- // Assign a quoted string to a StrType object.
- StrType StrType::operator=(char *s)
- {
- int len = strlen(s) + 1;
- if (size < len) {
- delete[] p;
- try {
- p = new char[len];
- }
- catch (bad_alloc xa) {
- cout << "Allocation error\n";
- exit(1);
- }
- size = len;
- }
- strcpy(p, s);
- return *this;
- }
- StrType StrType::operator=(const char *s)
- {
- int len = strlen(s) + 1;
- if (size < len) {
- delete[] p;
- try {
- p = new char[len];
- }
- catch (bad_alloc xa) {
- cout << "Allocation error\n";
- exit(1);
- }
- size = len;
- }
- strcpy(p, s);
- return *this;
- }
- // Concatenate two StrType objects.
- StrType StrType::operator+(StrType &o)
- {
- int len;
- StrType temp;
- delete[] temp.p;
- len = strlen(o.p) + strlen(p) + 1;
- temp.size = len;
- try {
- temp.p = new char[len];
- }
- catch (bad_alloc xa) {
- cout << "Allocation error\n";
- exit(1);
- }
- strcpy(temp.p, p);
- strcat(temp.p, o.p);
- return temp;
- }
- // Concatenate a StrType object and a quoted string.
- StrType StrType::operator+(char *s)
- {
- int len;
- StrType temp;
- delete[] temp.p;
- len = strlen(s) + strlen(p) + 1;
- temp.size = len;
- try {
- temp.p = new char[len];
- }
- catch (bad_alloc xa) {
- cout << "Allocation error\n";
- exit(1);
- }
- strcpy(temp.p, p);
- strcat(temp.p, s);
- return temp;
- }
- // Concatenate a quoted string and a StrType object.
- StrType operator+(char *s, StrType &o)
- {
- int len;
- StrType temp;
- delete[] temp.p;
- len = strlen(s) + strlen(o.p) + 1;
- temp.size = len;
- try {
- temp.p = new char[len];
- }
- catch (bad_alloc xa) {
- cout << "Allocation error\n";
- exit(1);
- }
- strcpy(temp.p, s);
- strcat(temp.p, o.p);
- return temp;
- }
- void StrType::operator+=(char * s){
- StrType temp;
- temp = p;
- size = size + strlen(s) + 1;
- p = new char[size];
- strcpy(p, temp.p);
- strcat(p, s);
- }
- void StrType::operator+=(StrType &o){
- StrType temp = p;
- size = size + o.size + 1;
- p = new char[size + o.size + 1];
- strcpy(p, temp.p);
- strcat(p, o.p);
- }
- StrType StrType::operator-(StrType &substr)
- {
- StrType temp(p);
- char *s1;
- int i, j;
- s1 = p;
- for (i = 0; *s1; i++) {
- if (*s1 != *substr.p) { // if not first letter of substring
- temp.p[i] = *s1; // then copy into temp
- s1++;
- }
- else {
- for (j = 0; substr.p[j] == s1[j] && substr.p[j]; j++);
- if (!substr.p[j]) { // is substring, so remove it
- s1 += j;
- i--;
- }
- else { // is not substring, continue copying
- temp.p[i] = *s1;
- s1++;
- }
- }
- }
- temp.p[i] = '\0';
- return temp;
- }
- // Subtract quoted string from a StrType object.
- StrType StrType::operator-(char *substr)
- {
- StrType temp(p);
- char *s1;
- int i, j;
- s1 = p;
- for (i = 0; *s1; i++) {
- if (*s1 != *substr) { // if not first letter of substring
- temp.p[i] = *s1; // then copy into temp
- s1++;
- }
- else {
- for (j = 0; substr[j] == s1[j] && substr[j]; j++);
- if (!substr[j]) { // is substring, so remove it
- s1 += j;
- i--;
- }
- else { // is not substring, continue copying
- temp.p[i] = *s1;
- s1++;
- }
- }
- }
- temp.p[i] = '\0';
- return temp;
- }
- bool StrType::insertstr(StrType &p, StrType &o, int position){
- string s, start, end, middle;
- StrType temp;
- s = p;
- start = s.substr(0, position);
- end = s.substr(position, size);
- s = o.p;
- middle = s.substr(0, o.size);
- s = start + middle + end;
- p = s.c_str();
- size = strlen(p) + 1;
- return true;
- };
- int find_char(StrType &p, char ch){
- for (int i = 0; i < p.strsize(); i++){
- if (p.p[i] == ch) {
- return i;
- }
- }
- return -1;
- }
- int main()
- {
- StrType s1("ab1231;2"), s2("ab");
- int position = find_char(s1, ';');
- cout << "Original string: " << s1 << endl;
- cout << "Number of symbols before ';' :" << position << endl;
- cout << "Number of symbols after ';' :" << s1.strsize() - position - 1 << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement