C++中回文类的帮助

C++中回文类的帮助,c++,C++,嗨,我想写一个回文类,但得到了错误的结果 我需要创建一个回文类并返回短语是否为回文。 这是我的密码。 回文 #ifndef PALINDROME_H #define PALINDROME_H #include <iostream> #include<cstring> using namespace std; class Palindrome{ private: char str[1024]; char s1[1024]; char s2[1024];

嗨,我想写一个回文类,但得到了错误的结果

我需要创建一个回文类并返回短语是否为回文。 这是我的密码。 回文

#ifndef PALINDROME_H
#define PALINDROME_H
#include <iostream>
#include<cstring>
using namespace std;

class Palindrome{
private:
   char str[1024];
   char s1[1024];
   char s2[1024];
   int a;
   int b;

public:
   Palindrome(char s2[1024], int a, int b)
   {
    s2[1024] = { 0 };
    a = 0;
    b = 0;
   }
   void removeNonLetters(char str[]);
   void lowerCase(char s1[]);
   bool isPalindrome(char s2[], int a, int b);
   }; // End of class definition


#endif
Palindrome.cpp:

#include "Palindrome.h"
void Palindrome::removeNonLetters(char str[])
{
    char s1[1024] = { 0 };

    int j = 0;
    int l1 = strlen(str);
    for (int i = 0; i < l1; i++)
    {
        if (str[i] <= '9' && str[i] >= '0')
       {
            s1[j++] = str[i];
       }
       else if ((str[i] >= 'A' && str[i] <= 'Z')
        || (str[i]) >= 'a' && str[i] <= 'z')
       {
            s1[j++] = str[i];
       }
     }
    cout << s1 << endl;
}
  void Palindrome::lowerCase(char s1[])
 {
       char s2[1024] = { 0 };
       int l2 = strlen(s1);
       int g = 0;
      for (int i = 0; i < l2; i++)
      {
          if (s1[i] >= 'a' && s1[i] <= 'z')
          {
             s2[g++] = s1[i];
          }
         else if (s1[i] >= 'A' && s1[i] <= 'Z')
        {
            s2[g++] = s1[i] + 32;
        }
    }
cout << s2 << endl;
}
bool Palindrome::isPalindrome(char s2[], int a, int b)
{
if (a >= b)
    return true;
    cout << "Yes" << endl;
if (s2[a] != s2[b])
   return false;
else
    return isPalindrome(s2, a + 1, b - 1); 
    cout << "No" << endl;
}
Main.cpp:

#include "Palindrome.h"
int main()
{
    char str[1024] = { 0 };
    char s1[1024] = { 0 };
    char s2[1024] = { 0 };
    cout << "input a string:" << endl;

    cin.getline(str, sizeof(str));

    Palindrome removeNonLetters(char str[]);

    Palindrome lowerCase(char s1[]);

    int length = strlen(s2);

    Palindrome isPalindrome(s2, 0, length - 1); 

    return 0;
}

你们老师可能不喜欢这样,但现实世界中我们就是这样做的

第一件事首先,接触标准库:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
将字符串转换为小写的函数:

std::string to_lower(std::string s)
{
    std::transform(std::begin(s),
                   std::end(s),
                   std::begin(s),
                   [](auto c) { return std::tolower(c); });
    return s;
}
一个函数,用于检查字符串的反向与正向是否相同:

bool is_palindrome(const std::string& s)
{
    return std::equal(std::begin(s), std::end(s),
                      std::rbegin(s), std::rend(s));
}
在测试中把所有这些放在一起:

int main()
{
    auto word = std::string("a!b B <>A");
    if (is_palindrome(to_lower(strip(word)))) {
        std::cout << "palindrome" << std::endl;
    }
    else {
        std::cout << "not palindrome" << std::endl;
    }

    return 0;
}
完整列表:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

std::string strip(std::string s)
{
    s.erase(std::remove_if(std::begin(s),
                           std::end(s),
                           [](auto c) { return !std::isalpha(c); }),
            std::end(s));
    return s;
}

std::string to_lower(std::string s)
{
    std::transform(std::begin(s),
                   std::end(s),
                   std::begin(s),
                   [](auto c) { return std::tolower(c); });
    return s;
}

bool is_palindrome(const std::string& s)
{
    return std::equal(std::begin(s), std::end(s),
                      std::rbegin(s), std::rend(s));
}

int main()
{
    auto word = std::string("a!b B <>A");
    if (is_palindrome(to_lower(strip(word)))) {
        std::cout << "palindrome" << std::endl;
    }
    else {
        std::cout << "not palindrome" << std::endl;
    }

    return 0;
}

你的代码有很多错误。我希望这些建议能有所帮助:

您应该使用std库。 为什么该类的构造函数采用任何参数?没有信号使用它们 为什么会有成员变量?什么都不用。 为什么函数在一个类中?它们只是函数-它们应该在函数库或类似的库中。 这些函数只是写入cout,因此是无用的。 您的主函数甚至似乎没有正确调用这些函数。 我试过这个:

char str[1024] = { 0 };
cout << "input a string:" << endl;

cin.getline(str, sizeof(str));

int length = strlen(str);
Palindrome a(str,0, length);
a.removeNonLetters(str);
a.lowerCase(str);
a.isPalindrome(str, 0, length - 1);

cin.getline(str, sizeof(str));
return 0;
然而,这也起作用:

input a string
hello
hello
hello
Yes

因此,如果删除空格也是有意的,那么前两个函数似乎可以工作,但第三个函数不行。

我建议您使用std::string而不是char数组。什么是错误的结果?请提供您的示例输入和输出。实际上,从所附的图像来看,您似乎没有得到任何结果,但存在运行时错误,对吗?RemovenOnletter和小写字母都是函数,而不是对象。它们是声明的,但从未定义过。用这个来上课是没有意义的。您可能应该阅读本书的前几章。崩溃的直接原因是一个包含1024个元素的数组的索引从0到1023,并且s2[1024]={0};在它之外写。这不是对字符数组的赋值,而是对字符的赋值。
input a string:
EVIL rats on no star **** live
EVILratsonnostarlive
evilratsonnostarlive
Yes
input a string
hello
hello
hello
Yes