Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- class String
- {
- private :
- static const int MAX_REF;
- char *charArray;
- int length;
- int *ref;
- void countChar();
- public :
- String();
- String(const char[]);
- String(const String&);
- ~String();
- char* getCharArray();
- int getLength();
- String operator+(String);
- String& operator=(const String&);
- void print();
- String toUpper(String);
- };
- const int String::MAX_REF = 26;
- String::String()
- {
- length = 0;
- charArray = NULL;
- ref = new int[MAX_REF];
- countChar();
- }
- String::String(const char paramCharArray[])
- {
- length = strlen(paramCharArray);
- charArray = new char[length];
- ref = new int[MAX_REF];
- for(int i = 0; i < length; i++)
- {
- charArray[i] = paramCharArray[i];
- }
- countChar();
- }
- String::String(const String &strSrc)
- {
- length = strSrc.length;
- charArray = new char[length];
- ref = new int[MAX_REF];
- for(int i = 0; i < length; i++)
- charArray[i] = strSrc.charArray[i];
- for(int i = 0; i < MAX_REF; i++)
- ref[i] = strSrc.ref[i];
- }
- String::~String()
- {
- delete ref;
- delete charArray;
- }
- char* String::getCharArray()
- {
- return charArray;
- }
- int String::getLength()
- {
- return length;
- }
- String String::operator+(String op)
- {
- String tmpStr;
- tmpStr.length = length + op.length;
- tmpStr.charArray = new char[tmpStr.length];
- int i;
- for(i = 0; i < length; i++)
- tmpStr.charArray[i] = charArray[i];
- for(int j = 0; j < op.length; i++, j++)
- tmpStr.charArray[i] = op.charArray[j];
- for(int j = 0; j < MAX_REF; j++)
- tmpStr.ref[j] = ref[j] + op.ref[j];
- return tmpStr;
- }
- String& String::operator=(const String &op)
- {
- if(this != &op)
- {
- delete ref;
- delete charArray;
- charArray = new char[length = op.length];
- ref = new int[MAX_REF];
- for(int i = 0; i < length; i++)
- charArray[i] = op.charArray[i];
- for(int i = 0; i < MAX_REF; i++)
- ref[i] = op.ref[i];
- }
- return *this;
- }
- void String::print()
- {
- for(int i = 0; i < length; i++)
- {
- cout << charArray[i] << endl;
- }
- /*for(int i = 0; i < nbValue; i++)
- {
- cout << "Nombre de ref : " << ref[i] << endl;
- }*/
- }
- void String::countChar()
- {
- char lowerChar;
- int j = 0;
- for(int i = 0; i < length; i++)
- {
- ref[j] = 0;
- lowerChar = tolower(charArray[i]);
- if(lowerChar >= 'a' && lowerChar <= 'z')
- {
- j = lowerChar - 'a';
- ref[j]++;
- }
- }
- }
- String String::toUpper(String strSrc)
- {
- String tmpSrc;
- for(int i = 0; i < strSrc.length; i++)
- tmpSrc.charArray[i] = toupper(strSrc.charArray[i]);
- return tmpSrc;
- }
- int main()
- {
- String test("!!!Hello World!!!"), test2, test3;
- test.print();
- test2 = test;
- cout << "------------------------" << endl;
- test2.print();
- test3 = test + test2;
- cout << "------------------------" << endl;
- test3.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement