Monday, July 22, 2024

Program 8 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 8 Design a class called cString to represent a string data type. Create a data member in the class to represent a string whose size is dynamically allocated. Write the following as member functions: a. Copy Constructor b. Destructor c. Concatenate two strings d. Find the length of the string e. Reversing a string f. Comparing two strings

 

BCA

Object Oriented Programming using C++ Practical

Madras University 

Program 8

Design a class called cString to represent a string data type. Create a data member in the class to represent a string whose size is dynamically allocated. Write the following as member functions: a. Copy Constructor b. Destructor c. Concatenate two strings d. Find the length of the string e. Reversing a string f. Comparing two strings
 
SOURCE CODE:

#include <iostream>
#include <cstring> // for strlen, strcpy, strcmp
using namespace std;

class cString {
private:
    char* str;

public:
    // Default constructor
    cString() : str(nullptr) {}

    // Parameterized constructor
    cString(const char* s) {
        str = new char[strlen(s) + 1];
        strcpy(str, s);
    }

    // Copy constructor
    cString(const cString& other) {
        str = new char[strlen(other.str) + 1];
        strcpy(str, other.str);
    }

    // Destructor
    ~cString() {
        delete[] str;
    }

    // Concatenate two strings
    void concatenate(const cString& other) {
        char* temp = new char[strlen(str) + strlen(other.str) + 1];
        strcpy(temp, str);
        strcat(temp, other.str);
        delete[] str;
        str = temp;
    }

    // Find the length of the string
    size_t length() const {
        return strlen(str);
    }

    // Reverse the string
    void reverse() {
        size_t len = strlen(str);
        for (size_t i = 0; i < len / 2; ++i) {
            std::swap(str[i], str[len - i - 1]);
        }
    }

    // Compare two strings
    int compare(const cString& other) const {
        return strcmp(str, other.str);
    }

    // Overload the assignment operator
    cString& operator=(const cString& other) {
        if (this == &other) {
            return *this;
        }

        delete[] str;
        str = new char[strlen(other.str) + 1];
        strcpy(str, other.str);
        return *this;
    }

    // Overload the stream insertion operator for easy printing
    friend std::ostream& operator<<(std::ostream& os, const cString& s) {
        os << s.str;
        return os;
    }
};

int main() {
    cString s1("Hello");
    cString s2("World");
    cString s3(s1);

    cout << "s1: " << s1 << endl;
    cout << "s2: " << s2 << endl;
     cout<< "s3: " << s3 << endl;

    s3.concatenate(s2);
    cout << "Concatenated s3: " << s3 << endl;

    cout << "Length of s3: " << s3.length() << endl;

    s3.reverse();
    cout << "Reversed s3: " << s3 << endl;

    int cmp = s1.compare(s2);
    cout << "Comparison of s1 and s2: " << cmp << endl;

    return 0;
}


OUTPUT:

s1: Hello
s2: World
s3: Hello
Concatenated s3: HelloWorld
Length of s3: 10
Reversed s3: dlroWolleH
Comparison of s1 and s2: -1

No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...