├── .gitattributes ├── .gitignore ├── MyString .h ├── MyStringImplimantation.cpp └── Q2main.cpp /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /MyString .h: -------------------------------------------------------------------------------- 1 | #ifndef MyString _H 2 | #define MyString_H 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MyString { 9 | private: 10 | char* strArr; 11 | int length; // store lengh of strArr including NULL 12 | MyString& stringSize(const char* p); // getstring and convert it in Mystring 13 | public: 14 | MyString(); //default constrctor 15 | MyString(char* p); //parameterized constrctor 16 | MyString(const MyString& p); // copy constrctor 17 | 18 | MyString operator +(const MyString p); 19 | MyString operator +(char *p); 20 | MyString operator -(MyString& p); 21 | MyString operator -(char *p); 22 | 23 | void operator =(const MyString p); 24 | void operator =(const char* p); 25 | 26 | bool operator ==(const MyString p); 27 | bool operator ==(const char* p); 28 | bool operator !=(const MyString p); 29 | bool operator !=(const char* p); 30 | bool operator <(const MyString p); 31 | bool operator <(const char* p); 32 | bool operator >(const MyString p); 33 | bool operator >(const char* p); 34 | 35 | char operator [](int p); 36 | 37 | friend ostream& operator<<(ostream& out, char *p);//output on console 38 | friend ostream& operator<<(ostream& out,const MyString& p); //output on console 39 | friend istream& operator>>(istream& in, MyString& p); // input from console 40 | friend ofstream& operator<<(ofstream& fout,const MyString& p); //output in file 41 | friend ifstream& operator>>(ifstream& fin, MyString& p);//input from file 42 | }; 43 | 44 | 45 | 46 | #endif // ! MyString _H 47 | -------------------------------------------------------------------------------- /MyStringImplimantation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"MyString .h" 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | MyString::MyString() { // default constructor 9 | strArr = NULL; 10 | length = 0; 11 | } 12 | MyString::MyString(char* p) { // parametarize constructor 13 | int size = 0; 14 | for (int i = 0; p[i] != '\0'; i++) { // find size 15 | size++; 16 | } 17 | length = size; 18 | strArr = new char[length + 1]; 19 | for (int i = 0; p[i] != '\0'; i++) { // copy data from one array to another 20 | strArr[i] = p[i]; 21 | } 22 | strArr[length] = '\0'; 23 | } 24 | MyString::MyString(const MyString& p) { // cope constructor 25 | length = p.length; 26 | if (p.strArr != NULL) { // if string not null 27 | strArr = new char[length + 1]; 28 | for (int i = 0; p.strArr[i] != '\0'; i++) { // copy data 29 | strArr[i] = p.strArr[i]; 30 | } 31 | strArr[length] = '\0'; 32 | } 33 | } 34 | MyString& MyString::stringSize(const char* p) { //use to find string size 35 | int size = 0; 36 | for (int i = 0; p[i] != '\0'; i++) { // find size 37 | size++; 38 | } 39 | MyString temp; 40 | temp.length = size; 41 | temp.strArr = new char[temp.length + 1]; 42 | return *this; 43 | } 44 | 45 | MyString MyString:: operator +(const MyString p) { // add two string 46 | MyString temp; 47 | temp.length = p.length + length; // length of two string 48 | temp.strArr = new char[temp.length + 1]; 49 | int i = 0; 50 | if (strArr == NULL && p.strArr!=NULL) { // if first NUll but second not NULL 51 | for (int j = 0; p.strArr[j] != '\0'; j++) { // copy of first string 52 | temp.strArr[j] = p.strArr[j]; 53 | } 54 | temp.strArr[temp.length] = '\0'; 55 | } 56 | else if (strArr != NULL && p.strArr != NULL) { // if first not NUll and second also not NULL 57 | for (; strArr[i] != '\0'; i++) { // copy data 58 | temp.strArr[i] = strArr[i]; 59 | } 60 | for (int j = 0; j(const char* p) { 234 | MyString temp = stringSize(p);// transfer normal string into MyString 235 | return (*this > temp); 236 | } 237 | 238 | bool MyString:: operator >(const MyString p) { // if both are class object 239 | if (length == p.length) { // length same of both strings 240 | return 0; 241 | } 242 | return !(*this < p); 243 | } 244 | 245 | char MyString:: operator [](int p) { 246 | if (strArr != NULL) { // if left stinr is not NULL 247 | if (p >= 0 && p < length) { //given number is in the range of index of string 248 | return strArr[p]; 249 | } 250 | } 251 | else { 252 | cout <<"Invalid Index"; 253 | } 254 | } 255 | 256 | ostream& operator<<(ostream& out, const MyString& p) { // // for class object 257 | if (p.length == 0) { // if string empty 258 | cout << "Empty"; 259 | return out; 260 | } 261 | else { 262 | for (int i = 0; p.strArr[i] != '\0'; i++) { // show output 263 | out << p.strArr[i]; 264 | } 265 | } 266 | return out; 267 | } 268 | 269 | ostream& operator<<(ostream& out, char *p) { // // for non class object 270 | for (int i = 0; p[i] != '\0'; i++) { // show out put 271 | out << p[i]; 272 | } 273 | return out; 274 | } 275 | 276 | istream& operator >> (istream& in, MyString& p) { // for class object 277 | if (p.length == 0) { // if string empty or NULl 278 | cout << "Enter size of string :"; 279 | in >> p.length; 280 | } 281 | else { 282 | delete[]p.strArr; 283 | p.length = -1; 284 | while(p.length <= 0) { // take size of string 285 | cout << "Enter size of string :"; 286 | in >> p.length; 287 | } 288 | } 289 | p.strArr = new char[p.length + 1]; 290 | char c; 291 | cout << "Enter String :"; 292 | for (int i = 0; i< p.length; i++) { // enter data from user 293 | c = getche(); 294 | if ((int)c != 13) { 295 | p.strArr[i] = c; 296 | } 297 | } 298 | p.strArr[p.length] = '\0'; 299 | cout << "\n"; 300 | return in; 301 | } 302 | 303 | ofstream& operator<<(ofstream& fout, const MyString& p) { //for class object 304 | if (p.length == 0) { // string empty 305 | fout << '0'; 306 | fout << endl << NULL; 307 | return fout; 308 | } 309 | else { 310 | fout <>(ifstream& fin, MyString& p) { // for class object and read data from file 318 | if (!fin.eof()) { //file not ended 319 | fin >> p.length; // take file length 320 | char c; 321 | fin >> c; 322 | if (p.strArr == NULL && c != NULL) { // first null but second not 323 | p.strArr = new char[p.length + 1];// allocat memory 324 | p.strArr[0] = c; 325 | for (int i = 1; i < p.length + 1; i++) { // get data from file 326 | fin.get(p.strArr[i]); 327 | } 328 | p.strArr[p.length] = '\0'; 329 | } 330 | else if (p.strArr != NULL && c != NULL) { //first not NULL and second also 331 | delete[] p.strArr; 332 | p.strArr = new char[p.length + 1]; // allocate memort 333 | p.strArr[0] = c; 334 | for (int i = 1; i < p.length + 1; i++) { //gat data from file 335 | fin.get(p.strArr[i]); 336 | } 337 | p.strArr[p.length] = '\0'; 338 | } 339 | else if (p.strArr != NULL && c == NULL) { //first not NUll but Second isNUll 340 | delete[]p.strArr; 341 | } 342 | } 343 | return fin; 344 | } 345 | 346 | ~MyString(){ 347 | delete[]strArr; 348 | strArr = NULL; 349 | } 350 | 351 | 352 | -------------------------------------------------------------------------------- /Q2main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include"MyString .h" 4 | #include 5 | 6 | using namespace std; 7 | 8 | int menu() { 9 | cout << endl; 10 | for (int i = 0; i < 40; i++) { // print upper line 11 | cout << char(223); 12 | } 13 | int operation = -1; 14 | cout << "\n 0 : Exist "; 15 | cout << "\n 1 : Input from counsole "; 16 | cout << "\n 2 : Input from file "; 17 | cout << "\n 3 : Output on counsole "; 18 | cout << "\n 4 : Output in file "; 19 | cout << "\n 5 : Sum two objects "; 20 | cout << "\n 6 : Sum object and string "; 21 | cout << "\n 7 : Subtract two objests "; 22 | cout << "\n 8 : Subtract object and string "; 23 | cout << "\n 9 : Check two objests are \"=\" "; 24 | cout << "\n 10 : Check objects and string are \"=\""; 25 | cout << "\n 11 : Check two objests are \"!=\""; 26 | cout << "\n 12 : Check objects and string are \"!=\""; 27 | cout << "\n 13 : Check which is \"<\" from two objects"; 28 | cout << "\n 14 : Check which is \"<\" from object and string "; 29 | cout << "\n 15 : Check which is \">\" from two objects"; 30 | cout << "\n 16 : Check which is \">\" from object and string "; 31 | cout << "\n 17 : clearScrean\n\t"; 32 | while(operation < 0 || operation > 17) { //in case of wrong input 33 | cin >> operation; 34 | if (operation < 0 || operation > 17) { 35 | cout << "\nWroung Input :"; 36 | } 37 | } 38 | return operation; 39 | } 40 | 41 | 42 | int main() { 43 | int operation = -1; 44 | bool isExit = false; 45 | char c[100]; 46 | MyString p1("feeleedsadsal khalid"); // first object 47 | MyString p2; // second object 48 | MyString p; // store final fting 49 | while (!isExit) { 50 | operation = menu(); // use to handle or select operation 51 | switch (operation) { 52 | case 0: { // exist from program 53 | isExit = true; 54 | break; 55 | } 56 | case 1: { //input from console 57 | cin >> p1>> p2; 58 | break; 59 | } 60 | case 2: {//input from file 61 | ifstream fin; 62 | fin.open("Input.txt"); 63 | fin >> p1, p2; 64 | fin.close(); 65 | break; 66 | } 67 | case 3: {//output on console 68 | cout << "\n p : " << p; 69 | cout << "\n p1 : " << p1; 70 | cout << "\n p2 : " << p2; 71 | break; 72 | } 73 | case 4: {//output in file 74 | ofstream fout; 75 | fout.open("Input.txt"); 76 | fout << p1 << p2; 77 | fout.close(); 78 | break; 79 | } 80 | case 5: {// add two class obj 81 | p = p1 + p2; 82 | break; 83 | } 84 | case 6: { // add one class obj and one simple string 85 | cout << "Enter string :"; 86 | for (int i = 0; (int)c[i - 1] != 13; i++) { 87 | c[i] = getche(); 88 | } 89 | p = p1 + c; 90 | break; 91 | } 92 | case 7: { //subtract two class obj 93 | p = p1 - p2; 94 | break; 95 | } 96 | case 8: { //sub class obj and sting 97 | cout << "Enter string :"; 98 | for (int i = 0; (int)c[i - 1] != 13; i++) { 99 | c[i] = getche(); 100 | } 101 | p = p1 - c; 102 | break; 103 | } 104 | case 9: {//chech two obj are equal 105 | if (p1 == p2) { 106 | cout << "\np1 and p2 are equall"; 107 | } 108 | if (p == p1) { 109 | cout << "\np and p1 are equall"; 110 | } 111 | if (p == p2) { 112 | cout << "\np and p2 are equall"; 113 | } 114 | break; 115 | } 116 | case 10: { // chech obj and string are equal 117 | cout << "Enter string :"; 118 | for (int i = 0; (int)c[i - 1] != 13; i++) { 119 | c[i] = getche(); 120 | } 121 | if (p == c) { 122 | cout << "\np and c are equall"; 123 | } 124 | if (p1 == c) { 125 | cout << "\np1 and c are equall"; 126 | } 127 | if (p2 == c) { 128 | cout << "\np2 and c are equall"; 129 | } 130 | break; 131 | } 132 | case 11: {//check two obj are not equal 133 | if (p1 != p2) { 134 | cout << "\np1 and p2 are not equall"; 135 | } 136 | if (p != p1) { 137 | cout << "\np and p1 are not equall"; 138 | } 139 | if (p != p2) { 140 | cout << "\np and p2 are not equall"; 141 | } 142 | break; 143 | } 144 | case 12: { //check obj and string are not equal 145 | cout << "Enter string in c :"; 146 | for (int i = 0; (int)c[i - 1] != 13; i++) { 147 | c[i] = getche(); 148 | } 149 | if (p != c) { 150 | cout << "\np and c are not equall"; 151 | } 152 | if (p1 != c) { 153 | cout << "\np1 and c are not equall"; 154 | } 155 | if (p2 != c) { 156 | cout << "\np2 and c are not equall"; 157 | } 158 | break; 159 | } 160 | case 13: { // check which obj is less 161 | if (p1 < p2) { 162 | cout << "\np1 is less than p2"; 163 | } 164 | if (p < p1) { 165 | cout << "\np is less than p1"; 166 | } 167 | if (p < p2) { 168 | cout << "\np is less than p2"; 169 | } 170 | break; 171 | } 172 | case 14: {check which obj is less than string 173 | cout << "Enter string in c :"; 174 | for (int i = 0; (int)c[i - 1] != 13; i++) { 175 | c[i] = getche(); 176 | } 177 | if (p < c) { 178 | cout << "\np is less than c"; 179 | } 180 | if (p1 < c) { 181 | cout << "\np1 is less than c"; 182 | } 183 | if (p2 < c) { 184 | cout << "\np2 is less than c"; 185 | } 186 | break; 187 | } 188 | case 15: { //check which obj is greater 189 | if (p1 > p2) { 190 | cout << "\np1 is greater than p2"; 191 | } 192 | if (p > p1) { 193 | cout << "\np is greater than p1"; 194 | } 195 | if (p > p2) { 196 | cout << "\np is greater than p2"; 197 | } 198 | break; 199 | } 200 | case 16: { //check which obj is greater than string 201 | cout << "Enter string in c :"; 202 | for (int i = 0; (int)c[i - 1] != 13; i++) { 203 | c[i] = getche(); 204 | } 205 | if (p > c) { 206 | cout << "\np is greater than c"; 207 | } 208 | if (p1 > c) { 209 | cout << "\np1 is greater than c"; 210 | } 211 | if (p2 > c) { 212 | cout << "\np2 is greater than c"; 213 | } 214 | break; 215 | } 216 | case 17: { // clear screen 217 | system("CLS"); 218 | break; 219 | } 220 | } 221 | } 222 | system("PAUSE"); 223 | return 0; 224 | } 225 | --------------------------------------------------------------------------------