1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include<iostream> #include<stack> using namespace std; void Reverse(char a[], int n) { stack<int> S; for (int i = 0; i < n; i++) { S.push(a[i]); } for (int i = 0; i < n; i++) { a[i] = S.top(); S.pop(); } } int main() { char a[20]; int n; cout << "请输入字符串:"; cin >> a; n = strlen(a); Reverse(a,n); cout << "反转后的字符串:"<<a; }
|