字符串插入

本文探讨了两种在代码中高效地插入字符串的方法,详细解释了每种方法的实现细节和适用场景,帮助开发者提升字符串处理能力。
摘要由CSDN通过智能技术生成

具体操作间代码中注释部分!!!
方法一

#include<stdio.h>
#include<string.h>
#define MAX 100
/*
在字符串str的第pos个位置之前插入另一个字符串T
pos从1开始,且 1<= pos <= strlen(str) + 1
例如:str = "hello", T = "world";
当 pos=1; 在字符串头前插入,即 worldhello;
当 pos = strlen(str) + 1; 在字符串末尾插入,即 helloworld
当 pos = 3; 在字符串中间插入,即 heworldllo
strinsert函数作用:
	先将下标为pos-1的字符一直到末尾的字符全部向后移动一位
	再将下标为pos-1的字符替换成'\0',上述pos=3时,即he\0llo\0
	先输出he,紧接着输出插入的字符串world,最后输出llo,即heworldllo
*/
void strinsert(char *str, int pos, char *T) {
   
	if (pos < 1 || pos > strlen(str) + 1)
		return 0;
	for (int i = strlen
  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值