標籤:資料結構
#include<stdio.h>#include<stdlib.h>#define MaxLength 60typedef struct{char str[MaxLength];int length;}SeqString;//串的賦值操作void StrAssign(SeqString *S,char cstr[]){int i;for(i=0;cstr[i]!='\0';i++)S->str[i]=cstr[i];//將常量cstr中的字元賦值給串SS->length=i;}//判斷串是否為空白,串為空白返回1,否則返回0int StrEmpty(SeqString S){if(S.length==0)return 1;elsereturn 0;}//求串的長度操作int StrLength(SeqString S){return S.length;}//串的複製操作void StrCopy(SeqString *T,SeqString S){int i;for(i=0;i<S.length;i++)//將串S的字元賦值給串TT->str[i]=S.str[i];T->length=S.length;//將串S的長度賦值給串T}//串的比較操作int StrCompare(SeqString S,SeqString T){//比較兩個串中的字元int i;for(i=0;i<S.length&&i<T.length;i++){//比較兩個串中的字元if(S.str[i]!=T.str[i])//如果出現字元不同,則返回兩個字元的差值return (S.str[i]-T.str[i]);}return (S.length-T.length);//如果比較完畢,返回兩個串的長度的差值}//串的插入操作。在S中第pos個位置插入T分為三種情況int StrInsert(SeqString *S,int pos,SeqString T){int i;if(pos<0||pos-1>S->length){//插入位置不正確,返回0printf("插入位置不正確");return 0;}if(S->length+T.length<=MaxLength){//第一種情況,插入子串後串長≤MaxLength,即子串T完整地插入到串S中for(i=S->length+T.length-1;i>=pos+T.length-1;i--)//在插入子串T前,將S中pos後的字元向後移動len個位置S->str[i]=S->str[i-T.length];for(i=0;i<T.length;i++)//將串插入到S中S->str[pos+i-1]=T.str[i];S->length=S->length+T.length;return 1;}else if(pos+T.length<=MaxLength){//第二種情況,子串可以完全插入到S中,但是S中的字元將會被截掉for(i=MaxLength-1;i>T.length+pos-i;i--)//將S中pos以後的字元整體移動到數組的最後S->str[i]=S->str[i-T.length];for(i=0;i<T.length;i++)//將T插入到S中S->str[i+pos-1]=T.str[i];S->length=MaxLength;return 0;}else{//第三種情況,子串T不能被完全插入到S中,T中將會有字元被捨棄for(i=0;i<MaxLength-pos;i++)//將T直接插入到S中,插入之前不需要移動S中的字元S->str[i+pos-1]=T.str[i];S->length=MaxLength;return 0;}}//在串S中刪除pos開始的len個字元int StrDelete(SeqString *S,int pos,int len){int i;if(pos<0||len<0||pos+len-1>S->length){printf("刪除位元置不正確,參數len不合法");return 0;}else{for(i=pos+len;i<=S->length-1;i++)S->str[i-len]=S->str[i];S->length=S->length-len;return 1;}}//串的串連操作int StrCat(SeqString *T,SeqString S){int i,flag;if(T->length+S.length<=MaxLength){for(i=T->length;i<T->length+S.length;i++)T->str[i]=S.str[i-T->length];T->length=T->length+S.length;flag=1;}else if(T->length<MaxLength){for(i=T->length;i<MaxLength;i++)T->str[i]=S.str[i-T->length];T->length=MaxLength;flag=0;}return flag;}//截取子串操作int SubString(SeqString *Sub,SeqString S,int pos,int len){int i;if(pos<0||len<0||pos+len-1>S.length){printf("參數pos和len不合法");return 0;}else{for(i=0;i<len;i++)Sub->str[i]=S.str[i+pos-1];Sub->length=len;return 1;}}//串的定位操作int StrIndex(SeqString S,int pos,SeqString T){int i,j;if(StrEmpty(T))return 0;i=pos;j=0;while(i<S.length&&j<T.length){if(S.str[i]==T.str[j]){i++;j++;}else{i=i-j+1;j=0;}}if(j>=T.length)return i-j+1;else return 0;}//串的替換操作int StrReplace(SeqString *S,SeqString T,SeqString V){//將S中所有的T替換為Vint i;int flag;if(StrEmpty(T))return 0;i=0;do{i=StrIndex(*S,i,T);//找到T在S中的位置if(i){StrDelete(S,i,StrLength(T));//刪除找到的Tflag=StrInsert(S,i,V);//在i位置插入Vif(!flag)return 0;i+=StrLength(V);}}while(i);return 1;}//串的清空操作void StrClear(SeqString *S){S->length=0;}//===========void StrPrint(SeqString S){int i;for(i=0;i<S.length;i++){printf("%c",S.str[i]);}printf("\n");}void main(){SeqString S1,S2,Sub;char ch[MaxLength];printf("請輸入第一個字串:\n");gets(ch);StrAssign(&S1,ch);printf("輸出串S1:");StrPrint(S1);printf("請輸入第二個字串:\n");gets(ch);StrAssign(&S2,ch);printf("輸出串S2:");StrPrint(S2);printf("將串S2插入到S1的第13個位置:\n");StrInsert(&S1,13,S2);StrPrint(S1);printf("將串S1中的第22個位置起的7個字元刪除:\n");StrDelete(&S1,22,7);StrPrint(S1);printf("將串S2中的第6個位置起的4字元取出放入Sub中:\n");SubString(&Sub,S2,6,4);StrPrint(Sub);printf("將串Sub賦值為America:\n");StrAssign(&Sub,"America");printf("將串S1中的串S2用Sub取代:\n");StrReplace(&S1,S2,Sub);StrPrint(S1);system("pause");}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
_DataStructure_C_Impl:順序串