当前位置:网站首页>串相关代码题--C语言
串相关代码题--C语言
2022-07-16 16:26:00 【Bwy_1004】
文章目录
一、将顺序串r中所有值为ch1的字符转换为ch2的字符
//替换串中指定元素
int StrReplaceX(SString *S,char ch1,char ch2){
for(int i=0;i<S->len;i++){
if(S->ch[i]==ch1)
{
S->ch[i]=ch2;
}
}
return 1;
}
二、将顺序串r中所有字符按照相反的次序仍存放在r中
法一:
//所有字符按照相反的次序仍存放在s中
void Reverse(SString *S){
char temp;
for(int i=0;i<(S->len)/2;i++){
temp=S->ch[i];
S->ch[i]=S->ch[S->len-i-1];
S->ch[S->len-i-1]=temp;
}
}
法二:利用栈结构
//利用栈逆置
void ReverseStack(SString *S,SeqStack *stack){
char data;
for(int i=0;i<S->len;i++){
Push(stack,S->ch[i]);
}
for(int j=0;j<S->len;j++){
Pop(stack,&data);
S->ch[j]=data;
}
}
三、从顺序串中删除所有值等于ch的字符
//从顺序串中删除所有值等于ch的字符
void deletech(SString *S,char ch){
int j=0;
for(int i=0;i<S->len;i++){
if(S->ch[i]!=ch){
S->ch[j]=S->ch[i];
j++;
}
}
S->len=j;
}
四、从顺序串r中删除所有与r1相同的子串
//从顺序串r中删除所有与r1相同的子串
void deletsub(SString *S,SString *Sub){
int flag =StrIndex(S,0,Sub);
while(flag!=-1){
//进行移位操作
for(int i=flag;i<S->len-Sub->len;i++){
S->ch[i]=S->ch[i+Sub->len];
}
S->len=S->len-Sub->len;
flag =StrIndex(S,flag,Sub);
}
}
//定位操作(修改)
int StrIndex(SString *S,int pos,SString *T){
int i=0,m=S->len,n=T->len;
SString Sub;
for(i=pos;i<m-n+1;i++){
SubString2(&Sub,S,i,n);
if(StrCompare(&Sub,T)==0){
return i;
}
}
return -1;
}
//求子串2
int SubString2(SString *Sub,SString *S,int pos,int len){
//是否越界
int j=0;
if(pos+len>S->len){
return 0;
}else{
for(int i=pos;i<pos+len;i++){
Sub->ch[j]=S->ch[i];
j++;
}
Sub->len=len;
return 1;
}
}
//串比较
int StrCompare(SString *S,SString *T){
for(int i=0;i<S->len&&i<T->len;i++){
if(S->ch[i]!=T->ch[i])
return S->ch[i]-T->ch[i];
}
return S->len-T->len;
}
五、编写一个函数将顺序串s1中的第i个字符到第j个字符之间的字符用s2串替换
//编写一个函数将顺序串s1中的第i个字符到第j个字符之间的字符用s2串替换
int replacei2j(SString *S1,int i,int j,SString *S2){
if(i>j){
printf("位置不合法!\n");
return 0;
}
if(j-i+1>S2->len){
int x=0;
for(int k=i-1;k<=j-1;k++){
S1->ch[k]=S2->ch[x];
x++;
if(x==S2->len){
x=0;
}
}
return 1;
}
else{
int x=0;
for(int k=i-1;k<=j-1;k++){
S1->ch[k]=S2->ch[x];
x++;
}
return 1;
}
}
六、实现顺序串的基本操作StrReplace(&s,t,v)
//StrReplace(&s,t,v)
int StrReplace(SString *S1,SString *Sub ,SString *S2){
//在主串中寻找子串
//确定起始位置和结束位置 调用 replacei2j
int flag =StrIndex(S1,0,Sub);
while(flag!=-1){
//找到第一次出现的子串 进行替换操作
replacei2j(S1,flag+1,flag+Sub->len,S2);
flag =StrIndex(S1,flag,Sub);
}
}
七、使用(链串)单链表实现模式匹配
//带头结点的单链表实现串的模式匹配算法
Link * SreIndex(LkString *S,LkString *T){
LinkNode *sp,*tp,*start;
sp=S->next;
tp=T->next;
start=S;
while(sp!=NULL && tp!=NULL){
if(sp->ch==tp->ch){
sp=sp->next;
tp=tp->next;
}else{
start=start->next;
sp=start;
tp=T->next;
}
}
if(tp==NULL) return start;
else return 0;
}
边栏推荐
- JMeter 21 day clock in day07
- Custom JSP tag
- Company equity distribution reference
- Open source! Hong Kong Chinese, MIT and Fudan put forward the first RNA cornerstone model
- 罗永浩:或2年后举行首场发布会,2000人年代码量是护城河
- 机器学习:交叉熵从理论到代码
- Dark horse programmer - software testing -15 stage 3- function testing 146-173 case review summary, tpshop project practice, state transition method cases, flow chart, business process testing, relati
- Win11 prompts what to do if this site is unsafe? Win11 prompt the solution of unsafe site
- 【深度学习】线上租用设备平台体验以及踩过的坑(非广告)
- Leetcode 1331. Array sequence number conversion
猜你喜欢

The third part of building PSIM simulation model of buck circuit (simulation of digital difference equation)

Luo Yonghao: or the first press conference will be held in two years. The age code of 2000 people is the moat

Machine learning: cross entropy from theory to code

Google Earth engine (GEE) -- the problem of over limit timeout extraction (applicable to besteffort)

Tear ORM by hand (generic + annotation + reflection)

Tool use -editor MD editor

满电出行王颖:用数据安全护航智慧出行

Anaconda安装(非常详细)

Airtest+poco multi script, multi device batch run test cases automatically generate test reports

Tensorflow speed entry
随机推荐
【CODETOOL】文件比较Beyond Compare使用介绍
5道String高频面试题,拿捏String底层原理!
公司股权结构设计的原则与因素
个性化推荐真的是“恶魔”吗?
「 每日一练,快乐水题 」1252. 奇数值单元格的数目
Custom JSP tag
"Daily practice, happy water problem" 1252 Number of odd value cells
五、单表查询优化
如何创建荷兰式拍卖智能合约
[tools] visual studio keymap
满电出行王颖:用数据安全护航智慧出行
Web.config中设置文件上传大小限制和请求数据流大小设置。
电子协会 C语言 2级 20 、 合法 C 标识符
How to remove the horizontal line in the header of computer word document
Day 4 training
Qt中的单例模式:实现一个单例的界面类
Tool use -editor MD editor
Ci521国产13.56MHz读写器芯片替代兼容CV520
JMeter 21 day clock in day05
BUCK 电路PSIM仿真模型搭建之三 (数字化差分方程的仿真)