首页 > 其他分享 >插入字符串,删除字符串——C语言描述

插入字符串,删除字符串——C语言描述

时间:2022-10-02 11:45:34浏览次数:79  
标签:Status Index int unsigned C语言 char 插入 TraStr 字符串

插入字符串,删除字符串——C语言描述

目录

0 测试用例框架

https://blog.csdn.net/m0_59469991/article/details/127137119?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22127137119%22%2C%22source%22%3A%22m0_59469991%22%7D

1 插入字符串

OP_STATUS StrInsert(char *Str, const unsigned int InSertPos, const char *InsertStr)

1.1功能需求:

​ 在主串Str的第InSertPos个位置的后面插入子串InsertStr

1.2 条件

Str与InsertStr非空

0<=InSertPos<=StringLen(Str) + 1

1.3方法

①先调MoveBackStr函数,移动字符

OP_STATUS MoveBackStr(char *Str, const unsigned int Pos, const int MvLen)

​ Str为数移动主串的字符,Pos为要移动的位置,MvLen为移动的长度,值为InsertStr的长度

②调用CopyStrWhithoutTail函数,插入字串

OP_STATUS CopyStrWhithoutTail(char *Str, const unsigned int Pos, const char *CpyStr)

Str为数移动主串的字符,Pos为要移动的位置,CpyStr为要插入的子串。

1.4 代码实现

①MoveBackStr函数

OP_STATUS MoveBackStr(char *Str, const unsigned int Pos, const int MvLen) {
	OP_STATUS Status = SUCCESS;
	char *TraStr = Str;
	unsigned int Index = 0;
	int TraPos = Pos - 1;
	unsigned int TraStrLen = StringLen(TraStr);

	int TraMvLen = MvLen;

	printf("MoveBackStr start\n");	

	if (TraStr == NULL || Pos > TraStrLen + 1) {
		Status = INVALID_PARAMETER;
		goto EXIT;
	}

	/*
	Example 1:
	Pos  = 1
	ab = 2
	RepLen = 4
	MvLen  = 4 - 2 = 2	

	Pos	
	0	1	2	3	4	5
	a	b	c	\0		
			a	b	c	\0
	----------------------------
	Example 2:
	Pos = 2
	bcde = 4
	RepLen = 2
	MvLen = 2 - 4 = -2

		Pos
	0	1	2	3	4	5	6
	a	b	c	d	e	f	\0
	a	d	e	f	\0	
	*/

	if (TraMvLen > 0) {
		for (Index = TraStrLen + 1 + TraMvLen; Index >= TraPos + TraMvLen; --Index) {
			TraStr[Index] = TraStr[Index - TraMvLen];
		}	
	} else if (TraMvLen < 0) {
		TraMvLen = 0 - TraMvLen;
		printf("TraMvLen = %d\n", TraMvLen);
		for (Index = TraPos; Index <= TraStrLen + 1 - TraMvLen; ++Index) {
			TraStr[Index] = TraStr[Index + TraMvLen];
		}
	}

	printf("TraStr = %s\n", TraStr);
EXIT:
	printf("MoveBackStr end\n");
	return Status;
}

②CopyStrWhithoutTail函数

OP_STATUS CopyStrWhithoutTail(char *Str, const unsigned int Pos, const char *CpyStr) {
	OP_STATUS		Status = SUCCESS;
	char			*TraStr = Str;
	char			*TraCpyStr = CpyStr;
	unsigned int	TraPos = Pos - 1;
	unsigned int	TraStrLen = StringLen(TraStr);
	unsigned int	TraCpStrLen = StringLen(TraCpyStr);
	unsigned int	Index = 0;

	printf("CopyStrWhithoutTail start\n");

	if (TraStr == NULL || Pos > TraStrLen + 1) {
		Status = INVALID_PARAMETER;
		goto EXIT;
	}

	for (Index = TraPos; Index < TraPos + TraCpStrLen; ++Index) {
		TraStr[Index] = *TraCpyStr;
		++TraCpyStr;
	}

	//for (Index = TraPos; Index < TraPos + TraCpStrLen; ++Index) {
	//	*(TraStr + Index) = *TraCpyStr;
	//	++TraCpyStr;
	//}

	printf("TraStr = %s\n", TraStr);
EXIT:
	printf("CopyStrWhithoutTail end\n");
	return Status;
}

③StrInsert函数

OP_STATUS StrInsert(char *Str, const unsigned int InSertPos, const char *InsertStr) {
	OP_STATUS Status = SUCCESS;
	char *TraStr = Str;
	unsigned int TraInSertPos = InSertPos;
	char *TraInserStr = InsertStr;
	unsigned int TraInsertLen = StringLen(TraInserStr);

	printf("StrInsert start\n");

	if (TraStr == NULL || TraInSertPos > StringLen(TraStr) + 1 || InsertStr == NULL) {
		Status = INVALID_PARAMETER;
		goto EXIT;
	}

	printf("TraInsertLen = %d\n", TraInsertLen);

	Status = MoveBackStr(TraStr, TraInSertPos, TraInsertLen);
	printf("TraStr = %s\n", TraStr);
	if (Status != SUCCESS) {
		goto EXIT;
	}

	Status = CopyStrWhithoutTail(TraStr, TraInSertPos, TraInserStr);
	printf("TraStr = %s\n", TraStr);

EXIT:
	printf("StrInsert end\n");
	return Status;
}

测试用例代码:

OP_STATUS StrInsert(char *Str, const unsigned int InSertPos, const char *InsertStr) {
	OP_STATUS Status = SUCCESS;
	char *TraStr = Str;
	unsigned int TraInSertPos = InSertPos;
	char *TraInserStr = InsertStr;
	unsigned int TraInsertLen = StringLen(TraInserStr);

	printf("StrInsert start\n");

	if (TraStr == NULL || TraInSertPos > StringLen(TraStr) + 1 || InsertStr == NULL) {
		Status = INVALID_PARAMETER;
		goto EXIT;
	}

	printf("TraInsertLen = %d\n", TraInsertLen);

	Status = MoveBackStr(TraStr, TraInSertPos, TraInsertLen);
	printf("TraStr = %s\n", TraStr);
	if (Status != SUCCESS) {
		goto EXIT;
	}

	Status = CopyStrWhithoutTail(TraStr, TraInSertPos, TraInserStr);
	printf("TraStr = %s\n", TraStr);

EXIT:
	printf("StrInsert end\n");
	return Status;
}

结果:

-------Test start----------

-------Test 01----------

SubString start

SubString end

Sub01 = a

StrCopmare start

Str1 = a

Str2 = a

ret = 0

StrCopmare end

StringCompareTest test succeed!

-------Test 02----------

SubString start

SubString end

Sub02 = bc

StrCopmare start

Str1 = bc

Str2 = bc

ret = 0

StrCopmare end

StringCompareTest test succeed!

-------Test result----------

Print test result;

TestNum = 2, PassNum = 2, FaildNum = 0

标签:Status,Index,int,unsigned,C语言,char,插入,TraStr,字符串
From: https://www.cnblogs.com/meditatorss/p/16748473.html

相关文章

  • 初识C语言
    #include<stdio.h>#include<stdlib.h>#include<math.h>intmain(){doublea,b,c,y;intx;scanf("%lf%lf%lf",&a,&b,&c);x=a+b+c;y=(a+b+c)/4;printf("x=%d",x);pri......
  • 【数据库】常用时间日期函数-日期加减、格式化、取指定部分、与字符串转换、取系统当
    一、日期格式化DATE_FORMAT(date,format):根据参数对date进行格式化format的格式: %Y年,数字,4位 %y年,数字,2位 %m月,数字(01……12) %d月份中的天数,数......
  • 数据结构 字符串 (第6天)
    这里的三题都和字符出现次数有关,可以用​​dict​​​或​​Counter​​​来轻松解决。和​​​ditc​​​相关的可以参照文档​​docscollections​​​,里面比较常用......
  • C语言常用函数
    数据类型:整数:char、unsignedchar(0-255)、signedchar(-128-127)、short、unsignedshort(0-65535)、int、unsignedint、long、unsignedlong小数:float(6位):单精度,eg:2.5fdouble......
  • 2022J T2 插入排序
    题目链接看到这个题第一反应肯定是模拟啊每次查询前都排一次查询后再复原很显然是超时的(但好像修改的时候进行排序和复原会更优一点)(反正都会超时无所谓了)考场也确实......
  • 字符串分割
    #include<iostream>#include<fstream>#include<string>usingnamespacestd;intmain(){ ifstreamreadfile; readfile.open("八卦图数据源.txt"); stringstr......
  • [LeetCode] 1316. Distinct Echo Substrings 不同的循环子字符串
    Returnthenumberof distinct non-emptysubstringsof text thatcanbewrittenastheconcatenationofsomestringwithitself(i.e.itcanbewrittenas......
  • 洛谷P3375 kmp字符串匹配
    #include<bits/stdc++.h>#defineintlonglongusingnamespacestd;inti,j,k,la,lb,kmp[1000005];chara[1000005],b[1000005];signedmain(){  scanf("%s%s",......
  • C语言猜单词游戏
    C语言猜单词游戏C语言猜单词游戏1问题描述请从一片英文的短文中任意提取一个单词,给出该单词的字母数量,让游戏者猜单词的拼写字母,游戏者每次只能猜一个字母,如果游戏者猜......
  • C语言学生的选课管理系统
    C语言学生的选课管理系统六、编制一个学生的选课管理系统【问题描述】现有若干个班级的学生,进行下学期课程的选课,任选5门课程录入课程信息(包括课程编号、课程名称、课......