实现C语言的拷贝函数且将复制后的字符串逆序,不能使用库函数,不能定义其他的变量。

面试被智商压制以后百度到的。

/*
  写C语言的拷贝函数,要求复制字符串,并且将复制后的字符串逆序 比如from中是1234, 
  则to中是4321 void strcyp(char * to,const char * from)问题补充:   
  要求: 不能使用库函数 不能定义其他的变量。
*/
#include <cstdio>
#include <cstdlib>

void strcyp(char *to, const char * from);

int main()
{
    char *in = "asdf!1234+567!@#$%^&*()_+8*90?";
    char *out;
    
    int len = 1;
    char *head;
    
    head = in;
    while (*head != '\0')
    {
        len++;
        head++;
    }
    
    out = (char *)malloc(sizeof(char) * (len + 1));
    strcyp(out, in);

    head = in;
    while (*head != '\0')
      printf("%c", *head++);
    printf("\n");
    
    head = out;
    while (*head != '\0')
      printf("%c", *head++);
    printf("\n");
    
    delete out;
    return 0;
}

void strcyp(char *to, const char * from)
{
    /*特殊情况*/
    if (from == NULL || *from == '\0'|| from == to)
		return -1;
    
    /*首先用to指针指向的当前位置保存from[0]*/  
    *to++ = *from++;
    
    /*定位from到最后一个字符,并且置to数组其余位为-1*/
    while (*from != '\0')
	{
		from++;
		*to++ = -1;
	}
    from--;
	*to-- = '\0';
    
    /*然后用from内元素对所有标记为'-1'位进行填充*/
    while (*to == -1)
		to--;
    
	while (*to != '\0')
		*to++ = *from--;
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值