Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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++ polindrome字符串的确定问题_C++ - Fatal编程技术网

C++ polindrome字符串的确定问题

C++ polindrome字符串的确定问题,c++,C++,给出了一项任务,需要确定输入的单词(或短语)是否为回文(这是一种可以从左到右和从右到左阅读的动词结构)。因此,找到回文这个词并没有给我带来任何特殊问题,下面是代码: #include <iostream> #include <string> #include <algorithm> using namespace std; bool polindrom(string pol) { int len = pol.length(); for (i

给出了一项任务,需要确定输入的单词(或短语)是否为回文(这是一种可以从左到右和从右到左阅读的动词结构)。因此,找到回文这个词并没有给我带来任何特殊问题,下面是代码:

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

using namespace std;

bool polindrom(string pol)
{
    int len = pol.length();
    for (int i = 0; i < len / 2; i++)
    {
        if (pol[i] != pol[len - i - 1])
        {
            return false;
        }
        return true;
    }
}

int main()
{
    string pol;
    cout << "Enter the phrase: " << endl;
    cin >> pol;
    if (polindrom(pol))
    {
        cout << "This phrase is a polindrome.";
    }
    else
    {
        cout << "This phrase is not a polindrome.";
    }
    cout << endl;
    system("pause");
} 
#包括
#包括
#包括
使用名称空间std;
bool polindrom(字符串pol)
{
int len=pol.length();
对于(int i=0;icout您使用的是
cin>
,它提取以空格分隔的输入

这是您的代码,稍加修改,可以逐个字符输入,忽略空格,将所有字母表转换为小写(忽略大小写),并在句点后丢弃所有内容。您可以实时查看代码

#包括
#包括
使用名称空间std;
布尔回文(字符串pol)
{
int len=pol.length();
对于(int i=0;i
您可以使用从输入流中读取整行

#包括
// ...
std::字符串行;
while(std::getline(std::cin,line))
{
//用线做点什么。。。
}
然后,“预处理”字符串,删除所有空格和点字字符,并将所有国会大厦字母转换为小写

#include <cctype>
// ...
std::string simplify(std::string const& str)
{//                              ^^^^^^ I don't want unnecessary copies
    std::string result;

    for (unsigned char ch : str)
    {
        if ( std::isspace(ch) or std::ispunct(ch) )
        {
            continue;
        }
        result.push_back(std::tolower(ch));
    }
    return result;
}
#包括
// ...
std::string simplify(std::string const&str)
{/^^^^^^^^我不想要不必要的副本
std::字符串结果;
for(无符号字符ch:str)
{
if(std::isspace(ch)或std::ispunct(ch))
{
继续;
}
结果:推回(标准:tolower(ch));
}
返回结果;
}
现在可以检查了

bool is_palindrome(std::string const& str)
{
    for (size_t i = 0, j = str.length(); i < j; ++i)
    {
        if ( str[i] != str[--j] )
        {
            return false;
        }
    }
    return true;
}
bool是回文(std::string const&str)
{
对于(尺寸i=0,j=str.length();i
put return true;out loop我建议你对你的
polindrom
函数进行一些调试。至少,但这也是学习如何使用调试器一步一步地调试代码语句的好时机。对不起,我无法抗拒:polindrom->palindrome;-)要消除bug,我想1.检查pol中的字符串。2.uppe如何r和小写字母已处理。可能还想查找
std::string(pol.rbegin(),pol.rend())
所做的事情,例如,抱歉,但是如何确保除了点之外排除所有标点符号。我不理解如果这样写:if(ispunt(c)){continue;}-所有标点符号(包括句点)将被忽略。您可以使用
if(c==”)
而不是
if(ispunt(c))
仅忽略句点。
#include <cctype>
// ...
std::string simplify(std::string const& str)
{//                              ^^^^^^ I don't want unnecessary copies
    std::string result;

    for (unsigned char ch : str)
    {
        if ( std::isspace(ch) or std::ispunct(ch) )
        {
            continue;
        }
        result.push_back(std::tolower(ch));
    }
    return result;
}
bool is_palindrome(std::string const& str)
{
    for (size_t i = 0, j = str.length(); i < j; ++i)
    {
        if ( str[i] != str[--j] )
        {
            return false;
        }
    }
    return true;
}