基本上,我成功地写了这个,我成功地反转了一个字!但是当我尝试反转包含 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;
}
最佳答案
问题不在于 char 数组与 std::string - 它在于输入法。
将 cin>>words
更改为 cin.getline(words, sizeof(words), '\n');
我猜这个任务是一个习惯数组的任务,所以坚持使用 char 数组 - 否则,是的,std::string 是易于使用的方法。
关于c++ - 读取字符串的长度,然后反转/反转字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13698719/