C++ 向后读取时需要检查字符串是否相同
我不知道这里出了什么问题。但我是个全新的人C++ 向后读取时需要检查字符串是否相同,c++,C++,我不知道这里出了什么问题。但我是个全新的人 #include <iostream> #include <stdio.h> #include <string.h> #include <locale.h> #include <tchar.h> #include <string> using namespace std; int main() { int string_length; string word,
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <tchar.h>
#include <string>
using namespace std;
int main()
{
int string_length;
string word, wordb;
cout << "Type in a word\n";
cin >> word;
string_length = word.length();
for (int i=1; i < (string_length+1); i++)
wordb = wordb + word.at(i);
if (word == wordb)
cout << "The word is the same in any direction.\n";
else
cout << "The word is not the same in any direction.\n";
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
int字符串长度;
字符串词,wordb;
cout>单词;
string_length=word.length();
对于(int i=1;i<(字符串长度+1);i++)
wordb=wordb+word.at(i);
if(word==wordb)
您不必从word
中“构造”wordb
,您可以在一次迭代中直接逐个比较字母:
for (int i=0; i < string_length; i++) {
if letter_in_pos[i] == letter_in_pos[string_length-i-1]
looks good, do nothing
else
break! word is not a palindrome!
}
for(int i=0;i
最后,如果这个单词是真正的回文,你只需要转到单词的中间(因为它是对称的)。最简单的方法是写
if ( word == string( word.rbegin(), word.rend() ) )
//...
或
对于您的方法,正确的循环将如下所示
for ( string::size_type i = word.length(); i != 0; )
wordb.push_back( word[--i];
或
chris建议使用std::equal
。下面是一个示例:
#include <string>
#include <algorithm>
#include <iostream>
bool is_palindrome(const std::string& s)
{
return std::equal(s.begin(), s.begin() + s.size()/2, s.rbegin());
}
int main()
{
std::string word;
std::cout << "Type in a word\n";
std::cin >> word;
if (is_palindrome(word))
std::cout << "The word is the same in any direction.\n";
else
std::cout << "The word is not the same in any direction.\n";
}
#包括
#包括
#包括
bool是回文(const std::string&s)
{
返回std::equal(s.begin(),s.begin()+s.size()/2,s.rbegin());
}
int main()
{
字符串字;
std::cout>word;
如果(是回文(单词))
std::cout我知道最简单的方法是使用简单的逻辑A[DeltaX]==B[-DeltaX],而DeltaX单词;
string_length=word.length();
//如果字符数小于2,则为真
if(字符串长度<2)
返回true;
最后一个字符=字符串长度-1;
对于(iTi=0;I<代码> i>代码>应该从0开始,而不是1。C++中的数组是零的。你不知道有什么不对吗?那么我们应该有什么线索?!你至少有一个错误消息吗?这应该会引发一个异常。读它说的和调试的。你尝试过调试它吗?你可以在调试器中设置变量的表表达式。你是CON。指令wordb
的顺序与word
的顺序相同。在固定索引(基于0,而不是基于1)后,考虑从word
的末尾开始访问字符,然后向后操作。是的,看起来不错,谢谢!@Pavel所以你应该更正你的答案,字母\u in_pos[i]==字母\u in_pos[string_length-i-1]或wordb=word;std::reverse(wordb.begin(),wordb.end());
无需为此编写自己的循环。@t它太复杂了。
for ( string::size_type i = word.length(); i != 0; )
wordb += word[--i];
#include <string>
#include <algorithm>
#include <iostream>
bool is_palindrome(const std::string& s)
{
return std::equal(s.begin(), s.begin() + s.size()/2, s.rbegin());
}
int main()
{
std::string word;
std::cout << "Type in a word\n";
std::cin >> word;
if (is_palindrome(word))
std::cout << "The word is the same in any direction.\n";
else
std::cout << "The word is not the same in any direction.\n";
}
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <tchar.h>
#include <string>
using namespace std;
int main()
{
int string_length;
int last_character;
bool found_mirror = true;
cout << "Type in a word\n";
cin >> word;
string_length = word.length();
// if the char count is less that 2 than technically it's true
if (string_length < 2)
return true;
last_character = string_length - 1;
for (int i = 0; i <= (string_length / 2); i++)
{
if (word[i] != word[last_character - i])
{
found_mirror = false;
break;
}
}
if (found_mirror)
cout << "The word is the same in any direction.\n";
else
cout << "The word is not the same in any direction.\n";
return 0;
}