【发布时间】:2012-12-04 08:26:12
【问题描述】:
基本上,我已经设法写了这个,并且我设法反转了一个单词!但是当我尝试反转包含 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() -
另外,
cin >> words将在第一个空白字符处停止解析。你需要使用类似getline()的东西。