Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串到字符 >所以我被要求编写一个C++程序来实现一个字符串对象。包含成员函数以比较两个字符串、连接两个字符串并确定字符串是否为回文_C++ - Fatal编程技术网

字符串到字符 >所以我被要求编写一个C++程序来实现一个字符串对象。包含成员函数以比较两个字符串、连接两个字符串并确定字符串是否为回文

字符串到字符 >所以我被要求编写一个C++程序来实现一个字符串对象。包含成员函数以比较两个字符串、连接两个字符串并确定字符串是否为回文,c++,C++,这是我的密码 #include <iostream> #include<string.h> using namespace std; class STR { public: void concatenateFunction(string s1, string s2); void compareFunction(string s1, string s2) ; void palindromeFunction(char

这是我的密码

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

class STR
{
    public:
        void concatenateFunction(string s1, string s2);
        void compareFunction(string s1, string s2) ;
        void palindromeFunction(char str[]);
};

void STR::compareFunction(string s1, string s2)
{
    // comparing both using inbuilt function
    int x = s1.compare(s2);
    cout<< "Compare:\n";
    if (x != 0)
        cout << s1 << " is not equal to "
             << s2 << endl;
    if (x > 0)
        cout << s1 << " is greater than "
             << s2 << endl;
    else
        cout << s2 << " is greater than "
             << s1 << "\n" << endl;
}

void STR::concatenateFunction(string s1, string s2, char c)
{
    string conc = s1 + s2;
    cout << "Concatenate:\n" << conc << "\n";

    std::string str = conc;

    char* c = strcpy(new char[str.length() + 1], str.c_str());
    return;
}

void STR:: palindromeFunction(char str[])
{
    int l = 0;
    int h = strlen(str) - 1;

    while (h > l)
    {
        if (str[l++] != str[h--])
        {
            cout << str << " is Not Palindrome";
            return;
        }
    }
    cout << str << " is Palindrome";
}

int main()
{
    STR res;
    char c;
    string s1,s2,conc;
    cout << "Enter a string 1: ";
    getline(cin, s1);

    cout << "Enter a string 2: ";
    getline(cin, s2);

    res.compareFunction(s1, s2);
    res.concatenateFunction(s1, s2);
    return 0;
}

如果你在concatenateFunction中看到,我试图将concatenate作为一个字符,这样我就可以执行回文函数。但由于它是std,所以显示错误。因此,他们有什么办法让我的代码运行。请提供帮助。

这可能不是要求您实现的字符串类

函数无效,只需打印即可


这个班没有成员。因此,这不是OOP,而是编写过程代码的另一种方式。

您的类应该是一种表示字符串的类型,就像std::string的基本版本一样

应该是这样的

class STR
{
    public:
        STR(const char* s);
        STR(const STR&);
        ~STR();
        STR& operator=(const STR& s);
        // Return the concatenation of this and 's'.
        STR concatenate(const STR& s) const;
        // Return -1, 0, or 1 if this is smaller, equal, or greater than 's' (like `strcmp`).
        int compare(const STR& s) const;
        // Retrun wheter this string is a palindrome.
        bool is_palindrome() const;
    private:
      // Suitable member variables.
};
你可以这样使用它:

int main()
{
    STR s1("hello");
    STR s2("goodbye");
    STR s3 = s1.concatenate(s2);
    if (s3.compare("hellogoodbye") == 0)
        std::cout << "good\n";
    else
        std::cout << "not good\n";
   std::cout << "Is s1 a palindrome? " << s1.is_palindrome();
}

请指出错误的确切行以及错误消息。另外,为什么要使用new和strcpy?string容器有更好的功能。函数返回void并只打印。这可能不是目的。问题和代码都与OOP无关。您的类没有成员,函数可能是静态的。您的类不表示字符串对象,它只是函数的集合,没有数据。忘记你的实际问题吧,你需要理解你被赋予的任务,因为你目前所拥有的将是失败的。谢谢你的评论。至少有一个明确的答案。你有什么网站可以推荐吗?你没有课本吗?这里有一些建议@AaronStoneA13看看这一条,不仅要阅读问题,还要阅读答案:试着从你的课堂笔记中找出基本的东西。祝你好运