Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by max ( 14 years ago )
class CStr {
    char* data;
    int len;
    static int count;

public:
    CStr();
    CStr(CStr &str;);
    CStr(char *str);
    CStr(char *str, unsigned int iLen);
    CStr(char ch, int count);
    void Print();
    int Find(char *pSubstr);
    CStr& Append(const CStr &str;);
    CStr Add(const CStr &str1;, const CStr &str2;);
};

#include "cstr.h"
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

CStr::CStr() {
    count++;
    data = (char*)calloc(1, sizeof(char));
    len = 0;
}

CStr::CStr(CStr &str;) {
    count++;
    this->len = str.len;
    this->data = (char*) calloc(this->len + 1, sizeof(char));
    memcpy(this->data, str.data, (this->len + 1) * sizeof(char));
}

CStr::CStr(char *str) {
    count++;
    this->len = strlen(str);
    this->data = (char*) calloc(this->len + 1, sizeof(char));
    memcpy(this->data, str, (this->len + 1) * sizeof(char));
}

CStr::CStr(char *str, unsigned int iLen) {
    count++;
    if (strlen(str) < iLen) exit(1);
    this->len = iLen;
    this->data = (char*) calloc(this->len + 1, sizeof(char));
    memcpy(this->data, str, (this->len + 1) * sizeof(char));
}
 CStr::CStr(char ch, int count) {
    count++;
    this->len = count;
    this->data = (char*) calloc(this->len + 1, sizeof(char));
    memset(this->data, ch, this->len);
 }

 void CStr::Print() {
    cout << this->data << endl;
 }

int CStr::Find(char *pSubstr) {
    char *p = strstr(this->data, pSubstr);
    if (!p) return -1;
    return (p - this->data);
}

CStr& CStr::Append(const CStr& str) {
    this->len += str.len;
    strcat(this->data, str.data);
    return *this;
}

 

Revise this Paste

Your Name: Code Language: