#include<iostream>
#include<cstring>
#include<stack> // C++标准库的stack
using namespace std;
void Reverse(char *C,int n)
{
stack<int> S;
//loop for push
for(int i = 0; i < n; i++)
{
S.push(C[i]);
}
//loop for pop
for(int i = 0; i < n; i++)
{
C[i] = S.top();
S.pop();
}
}
int main()
{
char C[51];
printf("Enter a string: ");
gets(C);
Reverse(C,strlen(C));
printf("Output = %s",C);
return 0;
}
时间复杂度:O(n)
空间复杂度:O(n)