字符串倒序
#include <stdio.h>
#include <string.h>
#include <stack>
char *Strrev(char *s)
{
char ch;
char *p = s;
char *q = s + strlen(s) - 1;
while (p != q)
{
ch = *p;
*p++ = *q;
*q-- = ch;
}
return s;
}
char *Strrev_Recursion(char *s, char *p)
{
if (s != p)
{
char ch = *s; *s = *p; *p = ch;
Strrev_Recursion(++s, --p);
}
return --s;
}
char *_Strev(char *s)
{
return Strrev_Recursion(s, s + strlen(s) - 1);
}
char *Strrev_WithoutRecursion(char *s)
{
using std::stack;
stack<char> aStack;
char *p = s;
while (*p != '/0')
aStack.push(*p++);
p = s;
while (!aStack.empty())
{
*p++ = aStack.top();
aStack.pop();
}
return s;
}
void main()
{
char s[50] = "I am a student.";
printf("%s/n", Strrev(s));
printf("%s/n", _Strev(s));
printf("%s/n", Strrev_WithoutRecursion(s));