我有一个for循环,它向后返回用户的输入。他们输入一个字符串,循环反转它。这里是什么样子:C++将For循环的输出分配给变量
string input; //what user enters
const char* cInput = input.c_str(); //input converted to const char*
for(int i = strlen(cInput) - 1; i >= 0; i--)
cout << input[i]; //Outputs the string reversed
而不必cout << input[i]
的,我怎么能设置input[i]
作为一个新的字符串的值?就像我想要一个名为string inputReversed
的字符串并将其设置为input[i]
。
换句话说,如果input == hello
和input[i] == olleh
,我想设置inputReversed
等于olleh
。
这是可行的吗?谢谢!
考虑使用['std :: string :: size'](http://www.cplusplus.com/reference/string/string/size/),而不是转换为'const char *'并使用'strlen'。 –