Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.1 kB
2
Indexable
Never
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;

class String {
public:
    char str[100];

    String(char s[]) {
        strcpy(str, s);
    }

    void operator=(String &s1) {
        strcat(str, s1.str);
    }

    int operator<=(String &s1) {
        return strcmp(str, s1.str);
    }

    String operator+(String &s1) {
        String result = *this;
        strcat(result.str, s1.str);
        return result;
    }

    void display() {
        cout << "String stored in class=" << str;
    }

    void display_length() {
        int count = 0;
        for (int i = 0; str[i] != '\0'; i++) {
            count++;
        }
        cout << "\nLength of string stored in class =" << count;
    }

    void To_Lower() {
        for (int i = 0; str[i] != '\0'; i++) {
            if (str[i] >= 'A' && str[i] <= 'Z') {
                str[i] = str[i] - 'A' + 'a';
            }
        }
        cout << "String converted to Lowercase: " << str << endl;
    }

    void To_Upper() {
        for (int i = 0; str[i] != '\0'; i++) {
            if (str[i] >= 'a' && str[i] <= 'z') {
                str[i] = str[i] - 'a' + 'A';
            }
        }
        cout << "String converted to Uppercase: " << str << endl;
    }
};

int main() {
    char ch[9] = "HELLO";
    char ch1[10] = "wOrLD";
    cout << "First String: ";
    String A(ch);
    A.display();
    A.display_length();
    A.To_Upper();
    A.To_Lower();

    cout << "\n\nSecond String: \n\n";
    String B(ch1);
    B.display();
    B.display_length();
    B.To_Lower();
    B.To_Upper();

    cout << "\n\nComparing String 1 and String 2" << endl;
    int ans = A <= B;

    if (ans == 0) {
        cout << "String 1 and String 2 are the same" << endl;
    } else if (ans > 0) {
        cout << "String 1 is greater than String 2" << endl;
    } else {
        cout << "String 1 is smaller than String 2" << endl;
    }

    cout << "\n\nCopying String 2 into String 1";
    A = B;
    cout << "\nString 1 is now: " << A.str;

    cout << "\n\nString Concatenated: " << endl;
    String C = A + B;
    C.display();

    return 0;
}
Leave a Comment