我正在创建这个revese字符串应用程序,但我得到一个错误,如果我在字符串中包含空格!反向字符串错误?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int inputa;
cout<<"%%%%%%%%%%%%%%%%%%String Reversing App%%%%%%%%%%%%%%%%%%%%%%%%"<<endl<<endl;
cout<<"\nEnter 1 to continue and 0 to exit"<<endl<<endl;
cin>>inputa;
if(inputa!=0)
{
do
{
string a,c="";
cout<<"\nEnter the string you want to Reverse : ";
cin>>a;
for(int x=a.length()-1; x>=0; x--)
{
c=c+a.substr(x,1);
}
cout<<"\nThe Reverse String is : "<<c<<endl;
cout<<"\nEnter 1 to continue and 0 to exit"<<endl<<endl;
cin>>inputa;
}
while(inputa!=0);
}
//not my home work
}
如果我输入像“abc def”这样的字符串,那么出现错误。但是,否则它完美的作品!代码有一些错误!我是CPP新手,所以如果你能帮助我的话,这将会有所帮助!
为什么不在'算法'库中使用'std :: reverse'? – Rapptz
你得到的错误是什么? –
@Scott Hunter我有时会得到一个无限循环,而下一次有其他的事情! –