Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSE 330 Lab 3: String Implement the class String that provides the string functi

ID: 3662754 • Letter: C

Question

CSE 330 Lab 3: String

Implement the class String that provides the string functionality needed by the test codes String_test.cpp and test.cpp given below. When you do this, you should not use the C++ stringclass. Also, in this implementation we will not use null terminated string of characters. Rather, we will maintain the size of the buffer. Use the following partial class definition:

String should be implemented in two files: String.h and String.cpp. The header file String.h should contain the class definition; the file String.cpp should contain implementations of the member functions. Compile as follows:

Explanation / Answer

As per given problem statement implemented the methods of Class String and tested with two cpp files
It is working String Class implemented functions definitions..and all the methods have comments to understand the code

#include <iostream>
#include <string.h>
using namespace std;

class String
{
        int size;
        char * buffer;
public:
        // Default String Constructor
        String()
       {
           buffer = "";
       }
      
       // Copy String Constructor with Reference
        String(const String &str)
       {
           size = strlen(str.buffer) + 1;
           buffer = new char[ size ];
           strcpy( buffer,str.buffer );
       }

       // Copy String Constructor with Pointer
       String(const char *s1)
       {
           if( s1 == NULL )
               s1 = "";
           size = strlen(s1) + 1;
           buffer = new char[size];
           strcpy( buffer, s1 );
       }

       // String Destructor
        ~String()
       {
           delete buffer;
       }

       // Compare two String Class objects equal or not
        friend bool operator==(const String &s1, const String &s2)
       {
           if(strcmp(s1.buffer,s2.buffer)==0)
               return true;
           else
               return false;
       }
      
       // Compare two String Class objects Greater than equal or not
        friend bool operator<=(const String &s1, const String &s2)
       {
            if(strcmp(s1.buffer,s2.buffer)<=0)
               return true;
           else
               return false;
       }
      
       // Compare two String Class objects Greater than or not
        friend bool operator<(const String &s1, const String &s2)
       {
            if(strcmp(s1.buffer,s2.buffer)<0)
               return true;
           else
               return false;
       }
      
       // Output stream object will print the String class buffer
       friend ostream & operator<<( ostream & out, const String & str)
       {
           return out << str.buffer;
       }
};