## @【数据结构】(字符串 基本操作函数)

@【数据结构】(字符串 基本操作函数)

字符串基本操作函数的编写:字符串连接、字符串比较、取长度、删除子串、字符串替换、定位子串取子串操作

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#define Max_Size 15
using namespace std;
typedef struct
{
	char buff[Max_Size];
	int len;
} StaticCString;
void Strcreate(StaticCString &s, char str[])
{
	int i;
	for (i = 0; str[i] != '\0'; i++)
		s.buff[i] = str[i];
	s.len = i;
}
//字符串连接
StaticCString concat(StaticCString s, StaticCString t)
{
	StaticCString r;
	int i, j;
	for (i = 0; i < s.len; i++)
		r.buff[i] = s.buff[i];
	for (j=0; j < t.len; j++)
		r.buff[i + j] = t.buff[j];
	r.len = i + j;
	return r;
}
//字符串比较
int compare(StaticCString s, StaticCString t)
{
	int i = 0;
	while (s.buff[i] == t.buff[i] && s.buff != '\0')    i++;
	return(s.buff[i] - t.buff[i]);
}
//取字符串长度
int length(StaticCString s)
{
	return(s.len);
}
void output(StaticCString s)
{
	int i;
	for (i = 0; i < s.len; i++)
		cout << s.buff[i];
	cout << endl;
}
//删除子串
void del(StaticCString &s, int i, int j)  //删除第i个位置起j个字符
{
	int k;
	if (i<1 || i>s.len || j<1 || i + j>s.len + 1) cout << "错误" << endl;
	else
	{
		for (k = i + j - 1; k < s.len; k++)
			s.buff[k - j] = s.buff[k];
		s.len = s.len - j;
	}
}
//定位子串
int index(StaticCString s, StaticCString t)
{
	int i = 0, j = 0;
	while (i < s.len&&j < t.len)
	{
		if (s.buff[i] == t.buff[j])
		{
			i++; j++;
		}
		else
		{
			i = i - j + 1; j = 0;
		}
	}
	if (j >= t.len)
		return i - t.len + 1;   //返回第一个字符的位置
	else return 0;
}
int insert(StaticCString &s, int i, StaticCString t)
{
	int j=0;
	if (i<1 || i>s.len + 1)  return 0;
	else
	{
		for (j=s.len-1;j>=i-1;j--)
			s.buff[j+ t.len] = s.buff[j];
		for (j = 0; j < t.len; j++)
			s.buff[i+j-1] = t.buff[j];
		s.len = s.len + t.len;
		return 1;
	}
}
//字符串替换
StaticCString replace(StaticCString &s, StaticCString s1, StaticCString s2)  // 将s中的字符串s1全部替换为s2
{
	int i;
	i = index(s, s1);
	while (i > 0)
	{
		del(s, i, s1.len);
		insert(s, i, s2);
		i = index(s, s1);
	}
	output(s);
	return s;
}
//取子串操作
void substr(StaticCString s, int i, int j)
{
	if (i<1 || i>s.len)
	{
		cout << "错误" << endl; 
	} 
	else
		for (int m = i-1; m < i + j - 1; m++)
			cout << s.buff[m];
}
void main()
{
	StaticCString s, t;
	int a;
	while (1)        //永真循环
	{
		cout << "---------------------------------------------" << endl;
		cout << "请选择操作:1:字符串连接 " << endl;
		cout << "           2:字符串比较 " << endl;
		cout << "           3:取字符串长度 " << endl;
		cout << "           4:删除子串" << endl;
		cout << "           5:字符串替换" << endl;
		cout << "           6:定位子串 " << endl;
		cout << "           7:取子串操作 " << endl;
		cout << "---------------------------------------------" << endl;
		cin >> a;
		switch (a)
		{
		case 1:
		{  
			char s1[] = "hello";
			char s2[] = "world";
			Strcreate(s, s1); Strcreate(t,s2);
			cout << s1 << "与" << s2 << "连接后:";
			output(concat(s, t));
			break;
		}
		case 2:
		{
			char s1[] = "hello";
			char s2[] = "he";
			Strcreate(s, s1); Strcreate(t, s2);
			cout<<s1<<"与"<<s2<<"比较" << compare(s, t);
			break;
		}
		case 3:
		{
			char s1[] = "hello";
			Strcreate(s, s1); 
			cout<<s1<<"字符串长度为:"<<length(s);
			break;
		}
		case 4:
		{
			char s1[] = "abcdefghi";
			Strcreate(s, s1);
			cout << s1<<"删除字符串第3个位置开始4个字符后";
			del(s, 3, 4); output(s);
			break;
		}
		case 5:
		{
			StaticCString m;
			char s1[] = "Thankyou";
			char s2[] = "you";
			char s3[] = "severyone";
			cout << "将字符串"<<s1<<"中的字符串" << s2 << "替换为字符串" << s3 << endl; 
			cout << "替换后:";
			Strcreate(s, s1); Strcreate(t, s2); Strcreate(m, s3);
			replace(s, t, m); ;
			break;
		}
		case 6: 
		{
			char s1[] = "Nice to meet you";
			char s2[] = "to";
			Strcreate(s, s1); Strcreate(t, s2);
			cout<<s2<<"在"<<s1<<"中位置为"<<index(s, t);
			break;
		}
		case 7:
		{
			char s1[] = "goodmorning";
			Strcreate(s, s1);
			cout << s1 << "字符串第2个位置起四个字符为:";
			substr(s, 2, 4);
			break;
		}
		}
		cout << endl;
	}
	system("pause");
}

测试示例:
删除子串:
【删除位置与元素个数都是主函数中确定的,可以自行修改,从键盘输入】
在这里插入图片描述
字符串替换:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值