C++ 回文程序中非常奇怪的问题
我必须用几个单词来测试它们是否是回文,不包括空格、、-“和”的字符C++ 回文程序中非常奇怪的问题,c++,C++,我必须用几个单词来测试它们是否是回文,不包括空格、、-“和”的字符 #包括 #包括 #包括 使用名称空间std; int main() { 整数输入; 对于(inti=0;i回文检查可以简单到 #include <iostream> #include <algorithm> #include <string> int main() { std::string str, rStr; std::cout << "Enter String
#包括
#包括
#包括
使用名称空间std;
int main()
{
整数输入;
对于(inti=0;i回文检查可以简单到
#include <iostream>
#include <algorithm>
#include <string>
int main() {
std::string str, rStr;
std::cout << "Enter String :\n";
std::cin >> str;
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
rStr = str;
std::reverse(begin(str), end(str));
(rStr == str) ? std::cout << "Word is palindrone\n" : std::cout << "Word is not palindrone\n";
return 0;
}
#包括
#包括
#包括
int main(){
std::字符串str,rStr;
std::cout>str;
std::transform(str.begin()、str.end()、str.begin()、::tolower);
rStr=str;
反转(开始(str),结束(str));
(rStr==str)?std::cout还可以展示一个与您的尝试截然不同的解决方案:
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string orig;
while (std::getline(std::cin, orig))
{
std::transform(orig.begin(), orig.end(), orig.begin(), ::toupper);
// erase the punctuation and spaces
orig.erase(std::remove_if(orig.begin(), orig.end(), [](char ch)
{ return ::isspace(ch) || ::ispunct(ch);}), orig.end());
std::cout << ((orig == std::string(orig.rbegin(), orig.rend()))?"Y":"N");
}
}
#包括
#包括
#包括
#包括
int main()
{
std::字符串源;
while(std::getline(std::cin,orig))
{
std::transform(orig.begin(),orig.end(),orig.begin(),::toupper);
//删除标点和空格
原始擦除(std::remove_if(orig.begin()、orig.end()、[](char ch)
{return::isspace(ch)|::ispunct(ch);}),orig.end();
STD:你是在测试每个单词还是一个单词序列。我不清楚边界是什么。如果你真的想用C++来做这个,使用算法,这是一个5行或6行程序,使用<代码> STD::“反转< /Cord>”和“代码> STD::ReaveVII:< /Cord>。怎么可能它不是回文?第一个输入是怎么可能的?”回文当第一个字符“A”不等于最后一个字符“A”,并且代码中没有将字母转换为同一大小写(全部大写或全部小写)的代码时这是一个信号,你应该调试你的代码。另外,一个完整的代码块丢失,使字母相同的外壳。提醒:在C++中,字母是区分大小写的。这意味着‘a’=‘a’。我真的想给你一个+1,但是@ Serdalis,如果在这个小的模块中使用,那不是坏的做法。这是个坏习惯。CE在头文件中使用时,其中包含头将无意中带来你不想要的东西。当它包含在C++源文件的顶端时,我仍然看到它咬人。主要是小助手函数等等。只是一个宠物恨我有和关闭话题,这个答案仍然很棒。(+现在1).@Serdalis没问题。反正我改了密码。问号有什么作用?
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string orig;
while (std::getline(std::cin, orig))
{
std::transform(orig.begin(), orig.end(), orig.begin(), ::toupper);
// erase the punctuation and spaces
orig.erase(std::remove_if(orig.begin(), orig.end(), [](char ch)
{ return ::isspace(ch) || ::ispunct(ch);}), orig.end());
std::cout << ((orig == std::string(orig.rbegin(), orig.rend()))?"Y":"N");
}
}