自己写全是错。
还是参考答案,学习一下。
string是c++的STL当中的一个容器,对其进行了封装。
#include<iostream>
#include<string>
using namespace std;
int main()
{
char c[100];
cin.get(c, 100);
int len1 = strlen(c);
for (int i = len1 - 1; i >= 0; i--)
{
cout << c[i];
}
return 0;
}
cin.get可以接收空格符,遇到回车结束。
但是PTA不通过,直接就编译错误。
标准答案是使用栈进行。
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<string> v;
string s;
while (cin >> s)
v.push(s);
cout << v.top();
v.pop();
while (!v.empty())
{
cout << " " << v.top();
v.pop();
}
return 0;
}
我看我是要疯了。
除了标准答案,其他全错。服了。