#include <iostream>
#include <cstring>
using namespace std;

class STR {
private:
    char *s;  // 指向待处理的字符串
    
    // 将指针 t1、t2 之间的字符前后逆序
    void backward(char *t1, char *t2) {
        while (t1 < t2) {
            char temp = *t1;
            *t1 = *t2;
            *t2 = temp;
            t1++;
            t2--;
        }
    }
    
public:
    // 构造函数,根据 t 参数初始化数据成员 s
    STR(char *t) {
        s = new char[strlen(t) + 1];
        strcpy(s, t);
        cout << "constructing..." << endl;
    }
    
    // 拷贝构造函数,拷贝参数 obj 的 s
    STR(const STR& obj) {
        s = new char[strlen(obj.s) + 1];
        strcpy(s, obj.s);
        cout << "copy constructing..." << endl;
    }
    
    // 利用函数 backward() 将字符串 s 中的各英文单词逆序
    void fun() {
        int len = strlen(s);
        int i = 0;
        
        while (i < len) {
            // 找到单词的起始位置(英文字母)
            while (i < len && !isalpha(s[i])) {
                i++;
            }
            int start = i;
            
            // 找到单词的结束位置
            while (i < len && isalpha(s[i])) {
                i++;
            }
            int end = i - 1;
            
            // 如果找到了一个单词,将其逆序
            if (start <= end) {
                backward(s + start, s + end);
            }
        }
    }
    
    // 输出数据成员
    void print() {
        cout << s;
    }
    
    // 析构函数,释放动态分配的内存
    ~STR() {
        delete[] s;
        cout << "delete space..." << endl;
    }
};

int main() {
    char input[1000];
    cin.getline(input, 1000);
    
    STR obj(input);
    STR objcopy(obj);
    
    obj.print();
    cout << " can be tranformed to ";
    objcopy.fun();
    objcopy.print();
    cout << endl;
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: