|
| 1 | +/*** default constructor; string s1; ***/ |
| 2 | +String :: String():str(nullptr), length(0){} |
| 3 | + |
| 4 | +/*** parameterised constructor; string s2("Ranjan") ***/ |
| 5 | +String :: String(const char* p){ |
| 6 | + length = mystrlen(p); |
| 7 | + str = new char[length+1]; |
| 8 | + mystrcpy(str, p, length); |
| 9 | +} |
| 10 | +String::String(String&& other) { |
| 11 | + // Transfer ownership of resources |
| 12 | + str = other.str; |
| 13 | + length = other.length; |
| 14 | + |
| 15 | + // Null out the source |
| 16 | + other.str = nullptr; |
| 17 | + other.length = 0; |
| 18 | +} |
| 19 | +/*** deep copy constructor; s2 = s1; ***/ |
| 20 | +String :: String(String& t){ |
| 21 | + length = t.length; |
| 22 | + //length = mystrlen(t.str); |
| 23 | + str = new char[length + 1]; |
| 24 | + mystrcpy(str, t.str, length); |
| 25 | +} |
| 26 | + |
| 27 | +/*** member function ***/ |
| 28 | +void String :: getstr(){ |
| 29 | + if(str==nullptr) return; |
| 30 | + cout<<str<<endl; |
| 31 | +} |
| 32 | +int String::len(){ |
| 33 | + return length; |
| 34 | +} |
| 35 | +char* String :: begin(void){ |
| 36 | + return str; |
| 37 | +} |
| 38 | + |
| 39 | +char* String :: end(void){ |
| 40 | + //length = strlen(str); |
| 41 | + return str+length-1; |
| 42 | +} |
| 43 | + |
| 44 | +/*** operator overloaded member function ***/ |
| 45 | +String& String :: operator =(String& s1){ // s2 = s1; |
| 46 | + length = s1.length; |
| 47 | + delete str; |
| 48 | + str = new char[length+1]; |
| 49 | + |
| 50 | + mystrcpy(str, s1.str, length); |
| 51 | + return *this; |
| 52 | +} |
| 53 | +String& String :: operator = (const char* ptr){ |
| 54 | + length = mystrlen(ptr); |
| 55 | + delete str; |
| 56 | + str = new char[length+1]; |
| 57 | + mystrcpy(str, ptr, length); |
| 58 | + |
| 59 | + return *this; |
| 60 | +} |
| 61 | +String& String::operator=(String&& other) { |
| 62 | + if (this != &other) { |
| 63 | + delete[] str; // Free existing memory |
| 64 | + str = other.str; // Steal the resource |
| 65 | + length = other.length; |
| 66 | + |
| 67 | + other.str = nullptr; // Nullify source |
| 68 | + other.length = 0; |
| 69 | + } |
| 70 | + return *this; |
| 71 | +} |
| 72 | +String String :: operator +(String& s1){ // +ope principal: doesn't modify its operands |
| 73 | + String result; |
| 74 | + result.length = length + s1.length; |
| 75 | + result.str = new char[result.length +1]; |
| 76 | + mystrcpy(result.str, str, result.length); |
| 77 | + |
| 78 | + for(int i=0; s1.str[i]; i++){ |
| 79 | + result.str[length + i] = s1.str[i]; // overwrite '\0' with first character of s2.str |
| 80 | + } |
| 81 | + result.str[result.length] = '\0'; |
| 82 | + return result; // call move constructor rather than copy |
| 83 | + |
| 84 | +} |
| 85 | +String& String :: operator +=(String& s1){ |
| 86 | + int i, j = length; |
| 87 | + length = length + s1.length; |
| 88 | + char* temp = new char[length + 1]; // str ko abhi free nhi kr skte |
| 89 | + mystrcpy(temp, str, length); |
| 90 | + for(i=0; s1.str[i]; i++){ |
| 91 | + temp[j++] = s1.str[i]; // overwrite '\0' with first character of s1.str |
| 92 | + } |
| 93 | + temp[j] = '\0'; |
| 94 | + delete str; // free resource pointed by str as content already copied to temp; |
| 95 | + str = temp; // str point the resource pointed by temp; |
| 96 | + temp = nullptr; // avoid temp to become dangling pointer |
| 97 | + return *this; // safely return the object reference |
| 98 | + |
| 99 | +} |
| 100 | +char& String :: operator [](int index){ |
| 101 | + if(str==nullptr){ |
| 102 | + str = new char[10]; |
| 103 | + return str[index]; |
| 104 | + } |
| 105 | + if (index < 0 || index > length) |
| 106 | + throw out_of_range("Index out of range"); |
| 107 | + return str[index]; |
| 108 | +} |
| 109 | +bool String :: operator >(String s2){ |
| 110 | + int i = 0; |
| 111 | + while(*(str+i) && *(s2.str+i)){ |
| 112 | + if(*(str + i) != *(s2.str + i)){ |
| 113 | + return *(str+i) > *(s2.str+i); |
| 114 | + } |
| 115 | + i++; |
| 116 | + } |
| 117 | + return *(str+i) > *(s2.str+i); |
| 118 | +} |
| 119 | +bool String :: operator <(String s2){ |
| 120 | + int i = 0; |
| 121 | + while(*(str+i) && *(s2.str+i)){ |
| 122 | + if(*(str + i) != *(s2.str + i)){ |
| 123 | + return *(str+i) < *(s2.str+i); |
| 124 | + } |
| 125 | + i++; |
| 126 | + } |
| 127 | + return *(str+i) < *(s2.str+i); |
| 128 | + |
| 129 | +} |
| 130 | +bool String :: operator >=(String s2){ |
| 131 | + int i = 0; |
| 132 | + while(*(str+i) && *(s2.str+i)){ |
| 133 | + if(*(str + i) != *(s2.str + i)){ |
| 134 | + return *(str+i) >= *(s2.str+i); |
| 135 | + } |
| 136 | + i++; |
| 137 | + } |
| 138 | + return *(str+i) >= *(s2.str+i); |
| 139 | + |
| 140 | +} |
| 141 | +bool String :: operator <=(String s2){ |
| 142 | + int i = 0; |
| 143 | + while(*(str+i) && *(s2.str+i)){ |
| 144 | + if(*(str + i) != *(s2.str + i)){ |
| 145 | + return *(str+i) <= *(s2.str+i); |
| 146 | + } |
| 147 | + i++; |
| 148 | + } |
| 149 | + return *(str+i) <= *(s2.str+i); |
| 150 | + |
| 151 | +} |
| 152 | +bool String :: operator != (String s2){ |
| 153 | + int i = 0; |
| 154 | + while(*(str+i) && *(s2.str+i)){ |
| 155 | + if(*(str + i) != *(s2.str + i)){ |
| 156 | + return *(str+i) != *(s2.str+i); |
| 157 | + } |
| 158 | + i++; |
| 159 | + } |
| 160 | + return *(str+i) != *(s2.str+i); |
| 161 | + |
| 162 | +} |
| 163 | +bool String :: operator ==(String s2){ |
| 164 | + int i = 0; |
| 165 | + while(*(str+i) && *(s2.str+i)){ |
| 166 | + if(*(str + i) != *(s2.str + i)){ |
| 167 | + return *(str+i) == *(s2.str+i); |
| 168 | + } |
| 169 | + i++; |
| 170 | + } |
| 171 | + return *(str+i) == *(s2.str+i); |
| 172 | + |
| 173 | + /*int i = 0; |
| 174 | + while(*(str+i)){ |
| 175 | + if(*(str+i) != *(s2.str +i)){ // s2.str; might unauthorize memory access after '\0' |
| 176 | + return 0; |
| 177 | + } |
| 178 | + i++; |
| 179 | + } |
| 180 | + if(*(str+i) == '\0' && *(s2.str + i) == '\0') |
| 181 | + return 1; |
| 182 | + return 0;*/ |
| 183 | +} |
| 184 | + |
| 185 | +/*** overloaded operator friend function ***/ |
| 186 | +ostream& operator <<(ostream& out, String& obj){ |
| 187 | + out<<obj.str; |
| 188 | + return out; |
| 189 | +} |
| 190 | +istream& operator >>(istream& in, String& obj){ |
| 191 | + if(obj.str == nullptr){ // String s4; cin>>s4; cout<<s4<<endl; |
| 192 | + |
| 193 | + fstream fin("input.txt", ios::in | ios::out | ios::trunc); |
| 194 | + if(!fin.is_open()) { |
| 195 | + cerr << "Error opening file\n"; |
| 196 | + exit(1); |
| 197 | + } |
| 198 | + |
| 199 | + char ch; |
| 200 | + int count = 0, i =0; |
| 201 | + |
| 202 | + // Read characters from user and store to file |
| 203 | + while(1) |
| 204 | + { |
| 205 | + cin.get(ch); |
| 206 | + fin.put(ch); |
| 207 | + if (ch == '\n') break; |
| 208 | + count++; |
| 209 | + } |
| 210 | + |
| 211 | + obj.str = new char[count+1]; |
| 212 | + obj.length = count; |
| 213 | + |
| 214 | + // Rewind file to beginning |
| 215 | + fin.seekg(0, ios::beg); |
| 216 | + while(1){ |
| 217 | + fin.get(ch); |
| 218 | + if(ch == '\n'){ |
| 219 | + obj.str[i] = '\0'; |
| 220 | + break; |
| 221 | + } |
| 222 | + obj.str[i++] = ch; |
| 223 | + } |
| 224 | + fin.close(); |
| 225 | + remove("input.txt"); // delete file to unnecessary fill hardisk |
| 226 | + return in; |
| 227 | + } |
| 228 | + in>>obj.str; |
| 229 | + return in; |
| 230 | +} |
| 231 | +String operator+(String& obj, const char* ptr){ |
| 232 | + obj.length = obj.length + mystrlen(ptr); |
| 233 | + String temp; |
| 234 | + temp.str = new char[obj.length + 1]; |
| 235 | + temp.length = obj.length; |
| 236 | + int i=0, j= 0; |
| 237 | + while(obj.str[i]){ |
| 238 | + temp.str[i] = obj.str[i]; |
| 239 | + i++; |
| 240 | + } |
| 241 | + while(ptr[j]){ |
| 242 | + temp.str[i++] = ptr[j++]; |
| 243 | + } |
| 244 | + temp.str[i]= '\0'; |
| 245 | + |
| 246 | + return temp; |
| 247 | +} |
| 248 | +String operator+(const char* ptr, String& obj){ |
| 249 | + String temp; |
| 250 | + temp.length = obj.length + mystrlen(ptr); |
| 251 | + temp.str = new char[obj.length + 1]; |
| 252 | + |
| 253 | + int i=0, j= 0; |
| 254 | + |
| 255 | + while(ptr[i]){ |
| 256 | + temp.str[i] = ptr[i]; |
| 257 | + i++; |
| 258 | + } |
| 259 | + while(obj.str[j]){ |
| 260 | + temp.str[i++] = obj.str[j++]; |
| 261 | + } |
| 262 | + temp.str[i]= '\0'; |
| 263 | + |
| 264 | + return temp; |
| 265 | +} |
| 266 | +String operator+(String& obj, const char ch){ |
| 267 | + String temp; |
| 268 | + temp.length = obj.length + 1; |
| 269 | + temp.str = new char[temp.length + 1]; |
| 270 | + int i = 0; |
| 271 | + while(obj.str[i]){ |
| 272 | + temp.str[i] = obj.str[i]; |
| 273 | + i++; |
| 274 | + } |
| 275 | + temp.str[i++] = ch; |
| 276 | + temp.str[i] = '\0'; |
| 277 | + |
| 278 | + return temp; |
| 279 | + |
| 280 | +} |
| 281 | +String operator+(const char ch, String& obj){ |
| 282 | + String temp; |
| 283 | + temp.length = obj.length + 1; |
| 284 | + temp.str = new char[temp.length + 1]; |
| 285 | + int i = 0, j=0; |
| 286 | + temp.str[i++] = ch; |
| 287 | + while(obj.str[j]){ |
| 288 | + temp.str[i++] = obj.str[j++]; |
| 289 | + } |
| 290 | + |
| 291 | + temp.str[i] = '\0'; |
| 292 | + |
| 293 | + return temp; |
| 294 | +} |
| 295 | + |
| 296 | +~String(){ |
| 297 | + delete[] str; |
| 298 | + cout<<"memory deallocated"<<endl; |
| 299 | + } |
0 commit comments