现在,有两个整数A和B,例如A是345,B是478,现在,需要把B插入到A里, 而A有三位,所以有四个位置选择,所得结果分别是: 478345, 347845, 344785, 345478 我们通过对比可以知道,在这当中最小的一个是344785

本文介绍了一个用于确定字符串连接位置的算法。该算法通过比较两个字符串的字符来决定将源字符串插入目标字符串的具体位置,旨在实现有序字符串的高效拼接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//-1代表在最前面插入

int Connect(char *source, char *dest)
{
    int len_s = 0;
    int len_d = 0;
    int i = 0, j = 0;
    int insPos = 0;

    len_s = strlen(source);
    len_d = strlen(dest);

    while(i < len_s)
    {
        if(*(source + i) < *(dest + j))
        {
            i++;
        }
        else if(*(source + i) > *(dest + j))
        {
            insPos = i -1;
            break;
        }else if(*(source + i) == *(dest + j))
        {
            if(*(source + i + 1) == *(dest + j))
            {
                i++;
                continue;
            }
           
            j++;
            while(j < len_d)
            {
                if(*(source + i) < *(dest +j))
                {
                    insPos = i;
                    break;
                }else if(*(source + i) > *(dest +j))
                {
                    insPos = i -1;
                    break;
                }
                else
                {
                    j++;
                }

            }

            if (j == len_d)
            {
                insPos = i;
            }
            break;
        }
    }

    if (i == len_s)
    {
    insPos = len_s - 1;
    }


    return insPos;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值