C++ 如何在不区分大小写的情况下检查文本文件的回文

C++ 如何在不区分大小写的情况下检查文本文件的回文,c++,visual-c++,C++,Visual C++,这是我当前的代码。它可以工作,但不能与单词中的大写字母一起工作,所以Anna不能工作。有没有关于如何忽略区分大小写的建议 #include <iostream> #include <fstream> #include <string> #include <stack> #include <queue> #include <algorithm> #include <ctype.h> using namespace

这是我当前的代码。它可以工作,但不能与单词中的大写字母一起工作,所以Anna不能工作。有没有关于如何忽略区分大小写的建议

#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#include <ctype.h>

using namespace std;


int main()
{
    ifstream infile;

    infile.open("mytext.txt");

    string line = "";

    do
    {

        std::stack<char>
            s(std::stack<char>::container_type(line.begin(), line.end()));
        std::queue<char>
            q(std::queue<char>::container_type(line.begin(), line.end()));

        while (!s.empty() && s.top() == q.front())
        {
            s.pop();
            q.pop();
        }

        if (s.empty()) std::cout << line << " The string is a palindrome" << std::endl;
    } while (getline(infile, line));

    return 0;
}

创建包含所有小写字母的轴字符串。使用std::tolower转换字符串

例如:

#include <stdio.h>
#include <ctype.h>
std::string str_tolower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), 
               [](unsigned char c){ return std::tolower(c); } 
              );
return s;
}

然后检查回文。

我认为唯一需要更改的是比较操作。您正在直接比较这两个字符。在比较之前,您需要先将它们转换为大写或小写

降低

使用Ascii码

如果你想让人觉得别致,你也可以试着把它转换成ascii表示,然后分别执行-65或-97

int s_top = (int(s.top()) < 97) ? int(s.top()) - 65 : int(s.top()) - 97;
int q_front = (int(q.front()) < 97) ? int(q.front()) - 65 : int(q.front()) - 97;
while (!s.empty() && s_top == q_front)
{
   s.pop();
   q.pop();
   s_top = (int(s.top()) < 97) ? int(s.top()) - 65 : int(s.top()) - 97;
   q_front = (int(q.front()) < 97) ? int(q.front()) - 65 : int(q.front()) - 97; 
}

97代表ASCII中的a,65代表a。

您希望逐行读取文件。对于要检查的每一行,如果一行是回文。另一个限制是检查应该不区分大小写。对于可能的解决方案之一,我们可以执行以下操作

打开文件,检查是否可以打开 读取文件中的所有行 创建一个包含所有小写字符的行的副本 检查包含所有小写字符的行的副本是否为回文 打印原始行作为结果 有关创建小写文件的其他说明:

我们使用STL:std::transform中的std算法。这样,我们迭代字符串中的每个字符,并使用内置函数tolower将其转换为小写

检测回文是一项简单的任务,我们只需将字符串与其反转版本进行比较。因此,我们将小写字符串与使用范围构造函数构造的临时std::string进行比较。对于范围,我们不使用标准的beginand end迭代器,而是使用std::rbegin和std::rend的反向版本。这是标准方法,非常简单

如果我们能找到回文,我们就打印出原始行

例如:

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

int main() {

    // Open the file
    std::ifstream file("mytext.txt");

    // If the file could be opened . . .
    if (file) {

        // Read all lines
        std::string line{};
        while (std::getline(file, line)) {

            // Derive a string with lowercase characters from read line
            std::string lcLine(line.size(),'\0');
            // Transform to lowercase
            std::transform(line.begin(), line.end(), lcLine.begin(), ::tolower);

            // Check, if we have a palindrome
            if (lcLine == std::string(lcLine.rbegin(), lcLine.rend())) {

                // Show the line with a palindrome
                std::cout << "Found a palindrome in:   " << line << "\n";
            }
        }
    }
    return 0;
}

我不确定我到底应该把这样的东西放在哪里,但它只是吐出一些错误,比如没有成员begin、end、begin和其他。我把它放在队列和回文检查之间,它是一个将字符串转换为小写的示例函数。你应该用这个想法来调整你的代码。Pro提示:如果你想有效地从两端删除元素,那么可以使用std::deque,这样你就不需要两个行的副本。你应该对答案进行投票,并决定接受哪一个复选标记。甚至不需要堆栈或队列。只需使用一个简单的循环进行迭代
int s_top = (int(s.top()) < 97) ? int(s.top()) - 65 : int(s.top()) - 97;
int q_front = (int(q.front()) < 97) ? int(q.front()) - 65 : int(q.front()) - 97;
while (!s.empty() && s_top == q_front)
{
   s.pop();
   q.pop();
   s_top = (int(s.top()) < 97) ? int(s.top()) - 65 : int(s.top()) - 97;
   q_front = (int(q.front()) < 97) ? int(q.front()) - 65 : int(q.front()) - 97; 
}
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

int main() {

    // Open the file
    std::ifstream file("mytext.txt");

    // If the file could be opened . . .
    if (file) {

        // Read all lines
        std::string line{};
        while (std::getline(file, line)) {

            // Derive a string with lowercase characters from read line
            std::string lcLine(line.size(),'\0');
            // Transform to lowercase
            std::transform(line.begin(), line.end(), lcLine.begin(), ::tolower);

            // Check, if we have a palindrome
            if (lcLine == std::string(lcLine.rbegin(), lcLine.rend())) {

                // Show the line with a palindrome
                std::cout << "Found a palindrome in:   " << line << "\n";
            }
        }
    }
    return 0;
}