基本上,我已经设法写出这个,并且设法颠倒了一个单词!但是当我尝试颠倒一个包含2个或更多字的字符串时,我无法获得输出。任何人都知道如何解决这个问题,或者是一些提示?读取字符串的长度,然后反转/反转字符串
#include <iostream>
using namespace std;
int main()
{
char words[100];
int x, i;
cout<<"Enter message : ";
cin>>words;
x = strlen(words);
//This two line is used to reverse the string
for(i=x;i>0;i--)
cout<<words[i-1]<<endl;
system("pause");
return 0;
}
为什么你使用C字符串,如果它应该是一个C++作业? –
为什么不使用'string'?将'char words [100]'改为'string words'并将'x = strlen(words)'改为'x = words.length()' – Maroun
此外,'cin >> words'将停止解析第一个空白字符。你需要使用像getline()这样的东西。 – Angew