1、使用指针
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
char *src="I am a student";
char *dest=NULL;
char *ss=NULL;
int i, len;
char *p1, *p2;
len = strlen(src);
p1 = src + (len - 1);
dest=(char *)malloc(len);
memset(dest, 0x0, sizeof(dest));
p2 = dest;
for (i = 0; i < len; ++i)
{
*p2++ = *p1--;
}
printf("dest: %s\n", dest);
return 0;
}
2、使用数组
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *dest=NULL ;
char *src="I am a student";
int len=strlen(src);
char temp1=len;
char temp2=0;
dest=(char*) malloc(len);
while(len--)
{
dest[temp2] = src[len];
temp2++;
}
printf("dest:%s\n",dest);
free(dest);
return 0;
}
3、使用地推函数
#include <stdio.h>
#include <string.h>
//最后一个元素下标
int end;
void turn(char *h,int p)
{
//到末尾时开始输出
if (p == end) printf("%c",h[p]);
else
{
//没到末尾则先输出下一位
turn(h,p+1);
//然后输出当前位
printf("%c",h[p]);
}
}
void main()
{
char* p = "I am a student";
end = strlen(p)-1;
turn(p,0);
printf("\n");
}