当前位置:网站首页>String related code questions -- C language
String related code questions -- C language
2022-07-18 19:44:00 【Bwy_ one thousand and four】
List of articles
- One 、 String the sequence r All values in are ch1 The characters of are converted to ch2 The characters of
- Two 、 String the sequence r All characters in the are still stored in r in
- 3、 ... and 、 Remove all values from the sequence string equal ch The characters of
- Four 、 From the sequence string r Delete all and r1 The same substring
- 5、 ... and 、 Write a function to string the sequence s1 No i Character to character j Characters between characters are used s2 String replacement
- 6、 ... and 、 Realize the basic operation of sequence string StrReplace(&s,t,v)
- 7、 ... and 、 Use ( Chain ) Single linked list realizes pattern matching
One 、 String the sequence r All values in are ch1 The characters of are converted to ch2 The characters of
// Replace the specified element in the string
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;
}
Two 、 String the sequence r All characters in the are still stored in r in
Law 1 :
// All characters are still stored in s in
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;
}
}
Law two : Using stack structure
// Using stack inversion
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;
}
}
3、 ... and 、 Remove all values from the sequence string equal ch The characters of
// Remove all values from the sequence string equal ch The characters of
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;
}
Four 、 From the sequence string r Delete all and r1 The same substring
// From the sequence string r Delete all and r1 The same substring
void deletsub(SString *S,SString *Sub){
int flag =StrIndex(S,0,Sub);
while(flag!=-1){
// To perform a shift operation
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);
}
}
// Positioning operation ( modify )
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;
}
// substring 2
int SubString2(SString *Sub,SString *S,int pos,int len){
// Is it across the border?
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;
}
}
// String comparison
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;
}
5、 ... and 、 Write a function to string the sequence s1 No i Character to character j Characters between characters are used s2 String replacement
// Write a function to string the sequence s1 No i Character to character j Characters between characters are used s2 String replacement
int replacei2j(SString *S1,int i,int j,SString *S2){
if(i>j){
printf(" Illegal location !\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;
}
}
6、 ... and 、 Realize the basic operation of sequence string StrReplace(&s,t,v)
//StrReplace(&s,t,v)
int StrReplace(SString *S1,SString *Sub ,SString *S2){
// Look for substrings in the main string
// Determine the start and end positions call replacei2j
int flag =StrIndex(S1,0,Sub);
while(flag!=-1){
// Find the substring that appears for the first time Carry out replacement operations
replacei2j(S1,flag+1,flag+Sub->len,S2);
flag =StrIndex(S1,flag,Sub);
}
}
7、 ... and 、 Use ( Chain ) Single linked list realizes pattern matching
// The single linked list of the leading node realizes the pattern matching algorithm of the string
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;
}
边栏推荐
- VIVADO 以太网接口(SGMII转GMII接口)
- 【CODETOOL】文件比较Beyond Compare使用介绍
- 树莓派关机重启命令
- Real masters are all practicing deliberately (recommended Collection)
- [MySQL] common commands and summary of "MySQL must know and know"
- Principles and factors of the design of the company's equity structure
- 3.2. Alexnet model
- Dataworks synchronizes data to the MySQL protocol compatible middleware, which is very slow, and there is no obvious acceleration after adding bandwidth. What should we do?
- Dynamic loading problem of PHP include tag file attribute (ThinkPHP)
- Custom JSP tag
猜你喜欢
随机推荐
罗永浩:或2年后举行首场发布会,2000人年代码量是护城河
Codeforces Round #807 (Div. 2)ABC
Logs issue 2022/07/16 | Li Jia, Hong Kong University of science and technology: Rethinking graph anomaly detection - what kind of graph neural network do we need?
Tensorflow speed entry
Optimisation des requêtes associées, optimisation des sept groupes de tri et analyse des requêtes d'interception
Unity 鼠标控制3d物体和UI的抓取移动(笔记)
[Solved]splunk the captain dose not share common baseline with 1 instance
Tool use -editor MD editor
工具使用-Editor.md编辑器
[codetool] file comparison beyond compare introduction
Web. Set the file upload size limit and request data stream size settings in config.
5、 Single table query optimization
BUCK 电路PSIM仿真模型搭建之三 (数字化差分方程的仿真)
内存函数的介绍及模拟实现
Luo Yonghao: or the first press conference will be held in two years. The age code of 2000 people is the moat
"Daily practice, happy water problem" 1252 Number of odd value cells
树莓派关机重启命令
[MySQL] common commands and summary of "MySQL must know and know"
Go language integer bisection template
Struct & Class & closure encapsulation in swift








