当前位置:网站首页>串的部分基本操作代码实现
串的部分基本操作代码实现
2022-07-19 20:21:00 【Luish Liu】
串的部分基本操作代码实现
定长顺序存储
#define MAXLEN 255 // 预定义最大串长
typedef struct
{
char ch[MAXLEN]; // 每个分量存储一个字符
int length; // 串的实际长度
} SString;
求子串
// 求子串。用 sub 返回串 S 中的第 pos 个字符起长度为len的子串
bool SubString(SString &sub, SString S, int pos, int len)
{
if((pos + len - 1) > S.length) // 子串范围越界
{
return false;
}
for(int i = pos; i < pos + len; pos++)
{
sub.ch[i - pos + 1] = S.ch[i];
}
sub.length = len;
return true;
}
比较操作
// 比较串 S 和串 T 的大小;若 S > T,返回值大于0;若 S < T,返回值小于0;若 S = T,返回值等于0
bool StrCompare(SString S, SString T)
{
for(int i = 1; i <= S.length && i <= T.length; i++)
{
if(S.ch[i] != T.ch[i])
{
return S.ch[i] - T.ch[i];
}
}
return S.length - T.length; // 扫描过的所有字符相同则长度长的串更大
}
定位操作
// 定位。若主串 S 中存在与串 T 值相同相同的子串,则返回他在主串 S 中第一次出现的位置;否则函数值为0
bool Index(SString S, SString T)
{
int i = 1, n = StrLength(S), m = StrLength(T);
SString Sub; // 暂存子串
while(i < (n-m+1))
{
SubString(sub, S, i, m);
if(StrCompare(S, T) != 0)
{
++i;
}
else
{
return i; // 返回子串在主串中的位置
}
}
return 0; // S中不存在和T相等的子串
}
边栏推荐
- Quels sont les écouteurs Bluetooth pour cadeaux? Top 10 Bluetooth Headset 2022
- Win10+libtorch+yolov5-6.0 deployment
- 索尼宣布关闭北京手机工厂!产线将迁至泰国,成本可降低一半!
- Oracle 里 TCP Socket (KGAS) 这个等待事件一般是什么导致?
- Apipost签约中国电信!携手加速企业数字化变革
- 干货 | 携程鸿蒙应用开发实践
- Redis 核心篇:唯快不破的秘密
- What is dbc2000? Dbc2000 database file details
- Redis core: the only secret that cannot be broken quickly
- Gson 学习笔记
猜你喜欢
The application could not be installed: INSTALL_FAILED_USER_RESTRICTED
Remote login ----- radius authentication
Apipost签约中国电信!携手加速企业数字化变革
Link list of daily Niuke questions
2022.07.18 洛谷 P6722 「MCOI-01」Village 村庄
最受IT公司欢迎的 30 款开源软件
Microblogging system based on BS architecture
Swagger 简单快速入门教程
What is dbc2000? Dbc2000 database file details
基于BS架构的微博系统
随机推荐
Remote login ----- radius authentication
LeetCode 69:爬楼梯 / 跳台阶
Redis 日志篇:无畏宕机快速恢复的杀手锏
[development tutorial 5] crazy shell arm function mobile phone serial port experiment tutorial
Unity3D学习笔记9——加载纹理
牛客每日刷题之数组
剑指Offer 71:跳台阶扩展问题
只列出指定字段的数据getdata($table, '*', $where, $order)
30 open source software most popular with IT companies
2022/07/19 learning notes (Day11) method overload
How to choose a 10000 person game server?
短視頻系統源碼,uni-app項目中主要文件的加載順序
CSAPP:cap2
Robotframework practice (III) derivation -- Take Baidu search function as an example to write test cases
SAP MM 事务代码MIGO 移动类型 561保存后报错-document number ### was already assigned
What is the function of dbc2000? Installation and configuration of dbc2000
Gson简单使用
About the list loop (five ways of writing foreach)
金仓数据库 KingbaseES SQL 语言参考手册 (3.10. 数据库对象引用方式)
Embedded sharing collection 16