thespeedracer38

String Concatenation Improved

Feb 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. class String{
  9.     char *str;
  10.     int size;
  11.     public:
  12.         String(const char* = "");
  13.         ~String();
  14.         int length();
  15.         bool operator >(String&);
  16.         bool operator <(String&);
  17.         bool operator ==(String&);
  18.         String operator +(const String&);
  19.         String operator+(const char *s);
  20.         friend istream& operator >>(istream&, String&);
  21.         friend ostream& operator <<(ostream&, String&);
  22. };
  23.  
  24. inline String String::operator+(const String &obj){
  25.     int len_1 = size;
  26.     int len_2 = obj.size;
  27.     int concat_len = len_1 + len_2 + 1;
  28.     char *temp = new char[concat_len];
  29.     strcpy(temp, str);
  30.     strcat(temp,obj.str);
  31.     String concat(temp);
  32.     delete[] temp;
  33.     return concat;
  34. }
  35.  
  36. inline String String::operator+(const char *s){
  37.     int len_1 = size;
  38.     int len_2 = strlen(s);
  39.     int concat_len = len_1 + len_2 + 1;
  40.     char *temp = new char[concat_len];
  41.     strcpy(temp, str);
  42.     strcat(temp, s);
  43.     String concat(temp);
  44.     delete[] temp;
  45.     return concat;
  46. }
  47.  
  48. inline String::String(const char *str){
  49.     String::str = new char[strlen(str) + 1];
  50.     strcpy(String::str, str);
  51.     size = strlen(str);
  52. }
  53.  
  54. inline String::~String(){
  55.     delete[] str;
  56.     str = NULL;
  57. }
  58.  
  59. inline int String::length(){
  60.     return size;
  61. }
  62.  
  63. inline bool String::operator >(String &obj){
  64.     return strcmp(str, obj.str) > 0;
  65. }
  66.  
  67. inline bool String::operator <(String &obj){
  68.     return strcmp(str, obj.str) < 0;
  69. }
  70.  
  71. inline bool String::operator ==(String &obj){
  72.     return strcmp(str, obj.str) == 0;
  73. }
  74.  
  75. istream& operator >>(istream &in, String &obj){
  76.     /* Try to do this in the constructor */
  77.     char temp[1024];
  78.     in.getline(temp, 1024);
  79.     delete[] obj.str;
  80.     obj.str = new char[strlen(temp) + 1];
  81.     strcpy(obj.str, temp);
  82.     obj.size = strlen(obj.str);
  83.     return in;
  84. }
  85.  
  86. ostream& operator <<(ostream &out, String &obj){
  87.     out << obj.str;
  88.     return out;
  89. }
  90.  
  91. int main() {
  92.     system("cls");
  93.     cout << "Enter the first string: ";
  94.     String str1;
  95.     cin >> str1;
  96.     cout << "Enter the second string: ";
  97.     String str2;
  98.     cin >> str2;
  99.     String str3 = str1 + " " + str2;
  100.     //String str3 = str1 + String(" ") + str2;
  101.     cout << str3;
  102.     cin.get();
  103.     return 0;
  104. }
Add Comment
Please, Sign In to add comment