Monday, May 9, 2022

Define a class String that could work as a user-defined string type. Include constructors that will enable us to create an uninitialized string String s1; and also to initialize an object with a string constant at the time of creation like String s2(“Well done”); Include a function that adds two strings to make a third string. Write a complete program to test your class to see that it does the following tasks : (a) Creates uninitialized string objects. (b) Creates objects with string constants. (c) Concatenates two strings properly. (d) Displays a desired string object.

 #include <iostream>

using namespace std;
#include <string.h>
class STRING
{
    char *str;
    int length;

public:
    STRING()
    {
        length = 0;
        str = new char[length + 1];
    }
    STRING(char *s)
    {
        length = strlen(s);
        str = new char[length + 1];
        strcpy(str, s);
    }
    void concat(STRING &A,STRING &B){
        length=A.length+B.length;
        str = new char[length + 1];
        strcat((strcpy(str,A.str)),B.str);
    }
    STRING(STRING &x){
        length=x.length+strlen(x.str);
        str = new char[length + 1];
        strcpy(str,x.str);
    }
    void display(){
        cout<<str<<endl;
    }
};
int main()
{
STRING A("hu"),B("hua");
STRING C;
C.concat(A,B);
C.display();
C=B;
C.display();

    return 0;
}

No comments:

Software scope

 In software engineering, the software scope refers to the boundaries and limitations of a software project. It defines what the software wi...

Popular Posts