几种字符串倒序输出

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");
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值