串的插入,删除,定位,连接等操作

匿名 (未验证) 提交于 2019-12-02 23:43:01
#include<stdlib.h> #include<iostream>   #define MAXLEN 20 #define TRUE 1 #define FALSE 0  using namespace std;  typedef struct SString{ char ch[MAXLEN]; int len; }SString;  //初始化串 void StrInit(SString *S) { char data; S->len=0; cout<<"请输入串,按#结束\n"; cin>>data; while(data!='#') {    S->ch[S->len]=data;    S->len++;    cin>>data; } S->ch[S->len]='\0'; }  //将T串插入到S串pos开始,S串后移 int StrInsert(SString *S,int pos,SString *T) { int i; if(pos<0||pos>S->len)return FALSE; if(S->len+T->len<=MAXLEN){    for(i=S->len+T->len-1;i>=T->len+pos;i--)     S->ch[i]=S->ch[i-T->len];    for(i=0;i<T->len;i++)     S->ch[pos+i]=T->ch[i];    S->len+=T->len; } else if(pos+T->len<=MAXLEN){    for(i=MAXLEN-1;i>=pos+T->len;i--)     S->ch[i]=S->ch[i-T->len];    for(i=0;i<T->len;i++)     S->ch[i+pos]=T->ch[i-T->len];    S->len=MAXLEN; } else{    for(i=pos;i<MAXLEN;i++)     S->ch[i]=T->ch[i-pos];    S->len=MAXLEN; } return TRUE; }  //删除S串中从pos开始的len个字符 int StrDel(SString *S,int pos,int len) { int i; if(pos<0||pos>(S->len-len))return FALSE; for(i=pos+len;i<S->len;i++)    S->ch[pos++]=S->ch[i]; S->len=S->len-len; return TRUE; }  //将串T拷贝到串S int StrCpy(SString *S,SString *T) { int i; for(i=0;i<T->len;i++){    S->ch[i]=T->ch[i]; } S->len=T->len; return TRUE; }  //判断串是否为空,为空返回TRUE,否则返回FALSE int StrEmpty(SString *S) { int flag; return S->len==0?flag=TRUE:flag=FALSE; }  //串比较函数 int StrCompare(SString *S,SString *T) { int i=0; if(S->len!=T->len)return FALSE; while(i<S->len) {    if(S->ch[i]!=T->ch[i])     return FALSE; } return TRUE; }  //求串的长度 int StrLen(SString *S) { return S->len; }  //清空串 int StrClear(SString *S) { S->len=0; return TRUE; }   //将串T连接到串S后 int StrCat(SString *S,SString *T) { int i; if(S->len+T->len<=MAXLEN){    for(i=0;i<T->len;i++)     S->ch[S->len+i]=T->ch[i];    S->len+=T->len; } else if(S->len<MAXLEN){    for(i=S->len;i<MAXLEN;i++)     S->ch[i]=T->ch[i-S->len];    S->len=MAXLEN; } else{    return FALSE; } return TRUE;  }  //求串S中pos开始, 串T第一次出现的位置 int StrIndex(SString *S,int pos,SString *T) { int i,j; if(T->len==0)return FALSE; i=pos;j=0; while(i<S->len&&j<T->len) {    if(S->ch[i]==T->ch[j]){i++;j++;}    else{     i=i-j+1;     j=0;    } } if(j>=T->len)return i-j; else return 0; }  void StrOutput(SString *S) { int i; for(i=0;i<S->len;i++)    i!=S->len-1?cout<<S->ch[i]<<" ":cout<<S->ch[i]<<"\n"; }   int main() { char cmd;int pos; int len;int index; do{    SString *S=(SString *)malloc(sizeof(SString));    SString *T=(SString *)malloc(sizeof(SString));    StrInit(S);StrInit(T);    cout<<"求串T在串S中从pos开始第一次出现的位置,输入pos\n";    cin>>pos;    index=StrIndex(S,pos,T);    cout<<"位置是"<<index<<"\n";    cout<<"输入插入到串S的位置pos\n";    cin>>pos;    StrInsert(S,pos,T);    StrOutput(S);    cout<<"输入要删除串S中开始的序号pos及字符长度len\n";    cin>>pos>>len;    StrDel(S,pos,len);    StrOutput(S);     cout<<"输入y/Y继续,其他退出\n";    cin>>cmd; }while(cmd=='y'||cmd=='Y');     return 0;  }

转载于:https://my.oschina.net/u/1167421/blog/546477

文章来源: https://blog.csdn.net/weixin_34191734/article/details/92080826
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!