数据结构与算法之逆序字符串

1

#include<stdio.h>
#include<string.h> 
int main()
{
	char ch[]="abcdefg";
	int len=strlen(ch);
	char *p=ch,*q=ch+len-1,temp;
	while(p<q)
	{
		temp=*p;
		*p=*q;
		*q=temp;
		++p;
		--q;
	}
	printf("%s",ch);
}

2

#include<stdio.h>
#include<string.h>
int main()
{
	char *ch="abcdefg";
	char buf[]="abcdefg" 
	int len=strlen(ch);
	char*p=buf,*q=buf+len-1,temp;
	while(p<q)
	{
		temp=*p;
		*p=*q;
		*q=temp;
		++p;
		--q;
	}
	printf("%s",buf);
}

3

//递归方法
#include<stdio.h>
int reverse(char*p)
{
	if(p==NULL)
	return -1;
	if(*p=='')
	return 0;
	reverse(p+1);
	printf("%c",*p);
}
int main()
{
	char *p="abcdefg";
	reverse(p);
 } 

4

#include<stdio.h>
#include<string.h>
int reverse(char*p,char *buf)

{

   int i=0;

   if(p==NULL)

          return -1;

   if(*p=='')

          return 0 ;

   reverse(p+1,buf);

   strncat(buf,p,1);

}
int main()

{

          char *p="abcdefg";

          char buf[1024]={0};

          reverse(p,buf);

          printf("%s",buf);

}

原文地址:https://www.cnblogs.com/AmosAlbert/p/12832363.html