C++ 回文程序帮助c++;
我在回文程序中翻转字符串时遇到问题。例如,我想输入“cat”,然后输出“tac”,但当我运行它时,它不会显示反向字符串,程序停止工作。有人能帮我吗C++ 回文程序帮助c++;,c++,C++,我在回文程序中翻转字符串时遇到问题。例如,我想输入“cat”,然后输出“tac”,但当我运行它时,它不会显示反向字符串,程序停止工作。有人能帮我吗 #include <iostream> #include <string.h> using namespace std; int main(){ string phrase1,phrase2; int len; cout<<"Please enter a word or phrase: "; cin>&
#include <iostream>
#include <string.h>
using namespace std;
int main(){
string phrase1,phrase2;
int len;
cout<<"Please enter a word or phrase: ";
cin>>phrase1;
len = phrase1.length();
for(int a=1;a<=len;a++){
phrase2[len-a] = phrase1[a-1];
}
cout<<endl<<phrase1<<endl<<phrase2<<endl;
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串短语1,短语2;
内伦;
措辞1;
len=短语1.length();
对于(int a=1;a您需要首先将短语2调整到正确的长度
phrase2.resize(len);
当你写这篇文章时:
for(int a=1;a<=len;a++){
phrase2[len-a] = phrase1[a-1];
}
for(int a=1;请在StackOverflow中搜索“c++回文”以查找回文中的许多示例。短语1可以工作并将显示“cat”,但不会显示短语2。相反,我会看到一个对话框,说.exe程序已停止工作。@ThomasMatthews有两种问题。一种是“我如何做X?”另一种是“我的代码到底出了什么问题?”。你的建议对第一个问题很好,但这个问题是第二类问题。你也可以使用调试器单步调试程序,找出问题所在。许多专业人士这样做,而不是将程序发布到StackOverflow上。string phrase1;cin>>phrase1;copy(phrase1.rbegin()、phrase1.rend()、ostream_迭代器(cout));
。但必须注意:使用名称空间std;
不好——您可能需要std::string…
,等等。