从界面上输入一个C风格的字符串,如果输入的是"abc",反转后"cba"。
要求:
1)反转的结果存放在另一字符串中。
2)原地反转,不借助其它的字符串。
#include <iostream>
using namespace std;int main()
{int var1 = 0;int var2 = 0;char str1[31];char str2[31];memset(str1, 0,sizeof(str1));memset(str2, 0,sizeof(str2));cout << "input str1 " << endl;cin >> str1;cout << "input length = " << strlen(str1) << endl;cout << "str1:" << &str1[0] << endl;for (int var1 = strlen(str1) - 1; var1 >= 0; var1--)//顺序{str2[var2++] =str1[var1];cout << "str1[" << var1 << "]=" << str1[var1] << endl;}str2[var2] = '\0';cout << "str2:" << str2 << endl;
/*------------------------------------
//---原地反转,不借助其他字符串int var1 = 0;char str1[31];memset(str1, 0,sizeof(str1));cout << "input str1 " << endl;cin >> str1;cout << "input length = " << strlen(str1) << endl;cout << "str1:" << &str1[0] << endl;for (int idx = 0; idx < strlen(str1) / 2; idx++){char tmp;tmp = str1[idx];str1[idx] = str1[strlen(str1) - idx-1];str1[strlen(str1) - idx-1] = tmp;}cout << "str1:" << str1 << endl;
---------------------------------------*/
}