#pragma once #include<iostream.h> #include<assert.h> #include<string.h> #include<stdlib.h> #define SIZE 255 typedef struct HString { char *ch; int length; }HString; void Show(); //菜单显示 void InitHString(HString *T);//初始化 void StrAssign(HString *T, char *str);//初始化字符串 void PrintString(HString *T); //显示字符串 int Length(HString *S); //求字符串长度 bool StrEmpty(HString *S);//判空 bool STrFull(HString *S);//判满 void StrCopy(HString *T, HString *S);//复制字符串 void DestroyString(HString *S); //摧毁字符串 int StrDelete(HString *T, int pos, int len); //按位置和长度删除字符串 void Replace(HString *T, HString *P, HString *V,int pos);//按位置替换字符串 int Index(HString *T, int pos, HString *P);//按位置匹配字符串 void StrInsert(HString *D, int pos, HString *T);//按位置插入字符串 int SubString(HString *D,HString *S, int pos, int len);//按位置获取字符串 void ClearString(HString *S); //清理字符串 int StrCompare(HString *T, HString *S);//比较字符串 void StringConcat(HString *T, HString *s1,HString *s2);//链接字符串
#include"HString.h" void Show() { cout<<"*********************************************"<<endl; cout<<"*[1]: 初始化字符串 [2]:拷贝字符串 *"<<endl; cout<<"*[3]: 比较字符串 [4]:求字符串长度 *"<<endl; cout<<"*[5]: 显示字符串 [6]:清理字符串 *"<<endl; cout<<"*[7]: 链接字符串 [8]:按位置得子串 *"<<endl; cout<<"*[9]: 匹配字符串 [10]:替换字符串 *"<<endl; cout<<"*[11]: 插入字符串 [12]:按位删除 *"<<endl; cout<<"*[13]: 摧毁字符串 [0]:结束程序 *"<<endl; cout<<"*********************************************"<<endl; cout<<"请选择:"; } int StrDelete(HString *T, int pos, int len) { if((pos+len)>T->length) return -1; char *ret=T->ch; int i=0; while(i<len)//abcdefg { ret[pos+i]=ret[i+pos+len]; i++; } return 1; } void StrInsert(HString *D, int pos, HString *T) { if(pos<0) return; char *ret=D->ch; for(int i=D->length;i>pos;i--) { ret[i+T->length]=ret[i]; } while(pos--) { ret++; } while(*T->ch!='\0') { *ret++=*T->ch++; } } void Replace(HString *T, HString *P, HString *V,int pos)//按位置替换字符串,用V字符串去替换 { int wei=Index(T,pos,P); if(wei==-1) { cout<<"没有找到合适的替换位置"<<endl; return; } char *ret=T->ch; while(*V->ch!='\0') { *ret++=*V->ch++; } } int Index(HString *T, int pos, HString *P)//按位置匹配,P是子串T是父串 { if(pos+P->length>T->length) return -1; int count=pos; while(count--) { T->ch++; } char *ret=T->ch; char *p=P->ch; int t_size=pos; int length=T->length-P->length; while(*p!='\0' && length>=P->length) { if(*ret==*p) { ret++; p++; } else { p=P->ch; ret=++T->ch; t_size++; length--; } } if(length<P->length) return -1; else return t_size; } int SubString(HString *D,HString *S, int pos, int len) { if(pos+len>S->length) return -1; int i=0; while(i++<pos) { S->ch++; } char *ret=S->ch; char *t=D->ch; while(len--) { *t++=*ret++; } *t='\0'; return 1; } void StringConcat(HString *T, HString *s1,HString *s2) { if(((s1->length)+(s2->length))>SIZE) return; char *ret=T->ch; while(*s1->ch!='\0') { *ret++=*s1->ch++; } while(*s2->ch!='\0') { *ret++=*s2->ch++; } *ret=*s2->ch; } void DestroyString(HString *S) { free(S->ch); S->ch=NULL; S->length=0; } void ClearString(HString *S) { *S->ch='\0'; S->length=0; } int Length(HString *S) { return S->length; } int StrCompare(HString *T, HString *S) { while(*T->ch==*S->ch) { T->ch++; S->ch++; } if(*T->ch>*S->ch) return 1; else if(*T->ch<*S->ch) return -1; else return 0; } void StrCopy(HString *T, HString *S) { if(STrFull(T)) { cout<<"空间已满"<<endl; return; } char *p=T->ch; while(*S->ch!='\0') { *p++=*S->ch++; } *p=*S->ch; } void InitHString(HString *T) { T->ch=(char*)malloc(sizeof(char)*SIZE); if(T->ch==NULL) { cout<<"开辟内存失败"<<endl; return; } T->length=0; } bool StrEmpty(HString *S) { return S->length==0; } bool STrFull(HString *S) { return S->length>=SIZE; } void StrAssign(HString *T, char *str) { if(STrFull(T)) { cout<<"内存已满"<<endl; return; } char *Str=T->ch; while(*str!='\0') { *Str++=*str++; T->length++; } *Str=*str; } void PrintString(HString *T) { char *str=T->ch; while(*str!='\0') { cout<<*str; str++; } cout<<endl; }
#include"HString.h" void main() { char *str="abcdefg"; char *ret="xy"; char *p="ggg"; HString S; HString TS; HString RS; InitHString(&S); InitHString(&TS); InitHString(&RS); int pos=1; int length=0; while(pos) { Show(); cin>>pos; switch(pos) { case 1: StrAssign(&S,str); StrAssign(&TS,ret); break; case 2: StrCopy(&TS,&S); cout<<"拷贝后的字符串:"; PrintString(&TS); break; case 3: pos=StrCompare(&S,&TS); cout<<"比较后的值:"<<pos<<endl; break; case 4: cout<<"字符串长度:"<<Length(&S)<<endl; break; case 5: PrintString(&S); break; case 6: ClearString(&S); break; case 7: StringConcat(&RS,&S,&TS); cout<<"链接后的字符串:"<<RS.ch<<endl; break; case 8: StrAssign(&S,str); cout<<"请输入位置:"; cin>>pos; cout<<"输入长度:"; cin>>length; pos=SubString(&RS,&S,pos,length); if(pos!=-1) cout<<"子串:"<<RS.ch<<endl; else cout<<"输入位置和长度不合理"<<endl; break; case 9: StrAssign(&S,str); StrAssign(&TS,ret); cout<<"输入位置:"; cin>>pos; pos=Index(&S,pos,&TS); if(pos!=-1) cout<<"在"<<pos<<"位置匹配成功"<<endl; else cout<<"匹配不成功"<<endl; break; case 10: StrAssign(&S,str); StrAssign(&TS,ret); StrAssign(&RS,p); cout<<"输入位置:"; cin>>pos; Replace(&S,&TS,&RS,pos); cout<<S.ch<<endl; break; case 11: StrAssign(&S,str); StrAssign(&TS,ret); cout<<"输入位置:"; cin>>pos; StrInsert(&S,pos,&TS); cout<<S.ch<<endl; break; case 12: StrAssign(&S,str); cout<<"请输入位置:"; cin>>pos; cout<<"输入长度:"; cin>>length; pos=StrDelete(&S,pos,length); if(pos!=-1) cout<<S.ch<<endl; else cout<<"位置或长度不合适,不能完成删除"<<endl; break; case 13: DestroyString(&S); cout<<"字符串已被摧毁"<<endl; break; // case 0: exit(-1); default: break; } } }
原文地址:http://blog.csdn.net/zhou753099943/article/details/46289383