# 字符串倒序

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/15124.html | 发布：2022-12-29 | 更新：2022-12-29 | 分类：建站教程、投稿分享

字符串倒序

 

#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));

 

![字符串倒序](https://www.mfisp.com/wp-content/uploads/2022/10/11048-apQ5nb.jpg)

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
