串的應用舉例
阿新 • • 發佈:2017-06-13
font 賦值 spa -a jsb subst 不能 bsp lag
如果串S1 = "I come from Beijing",S2 = "Chongqing" ,Sub = "America".
利用串的基本操作,假設串的賦值、串的插入、串的刪除、串的替換、對上面
的串進行操作
利用串的基本操作,假設串的賦值、串的插入、串的刪除、串的替換、對上面
的串進行操作
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 60 typedef struct { char str[MAXSIZE]; int length; }SeqString; void StrAssign(SeqString *S,char cstr[]);//串的賦值操作 int StrEmpty(SeqString S);//推斷串是否為空 int StrLength(SeqString S);//求串的長度操作 void StrCopy(SeqString *T,SeqString S);//串的復制操作 int StrCompare(SeqString S,SeqString T);//串的比較操作 int StrInsert(SeqString *S,int pos,SeqString T);//串的插入操作 int StrDelete(SeqString *S,int pos,int len);//串的刪除操作 int StrConcat(SeqString *T,SeqString S);//串的連接操作 int SubString(SeqString *Sub,SeqString S,int poos,int len);//截取子串操作 int StrReplace(SeqString *S,SeqString T,SeqString V);//串的替換操作 int StrIndex(SeqString S,int pos,SeqString T);//串的定位操作 void StrClear(SeqString *S);//清空串操作 void StrPrint(SeqString S);//串的輸出聲明 #include "string.h" int main(void) { SeqString S1,S2,Sub; char ch[MAXSIZE]; 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); return 0; } #include "string.h" void StrAssign(SeqString *S,char cstr[])//串的賦值操作(將常量cstr中的字符賦值給串S) { int i = 0; for(i = 0;cstr[i]!=‘\0‘;i++) { S->str[i] = cstr[i]; } S->length = i; } int StrEmpty(SeqString S)//推斷串是否為空 { if(S.length == 0) { return 1; } else { return 0; } } int StrLength(SeqString S)//求串的長度操作 { return S.length ; } void StrCopy(SeqString *T,SeqString S)//串的復制操作(將串S中的每個字符賦給T) { int i; for(i = 0;i < S.length ;i++) { T->str[i] = S.str[i]; } T->length = S.length ; } 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);//假設比較完成。返回兩個字符串的長度的差值 } int StrInsert(SeqString *S,int pos,SeqString T)//串的插入操作(在串S的pos個位置插入串T) { int i; if(pos < 0 || pos-1 > S->length) { printf("插入位置不對\n"); return 0; } if(S->length + T.length <= MAXSIZE)//子串完整插入到串中 { //在插入子串T前,將S中的pos後的字符向後移動len個位置 for(i = S->length+T.length-1;i >= pos+T.length-1;i--) { S->str[i] = S->str[i-T.length]; } //將串插入到S中 for(i = 0;i < T.length ;i++) { S->str[pos+i-1] = T.str[i]; } S->length = S->length +T.length ; return 1; } else if(pos +T.length <= MAXSIZE)//子串全然插入S中,可是S中的字符會被截斷 { for(i = MAXSIZE-1;i > T.length +pos-1;i--) { S->str[i] = S->str[i-T.length]; } for(i = 0;i < T.length ;i++) { S->str[i+pos-1] = T.str[i]; } S->length = MAXSIZE; return 0; } else//子串T不能全然插入到S中。T將會有字符被舍棄 { for(i = 0;i < MAXSIZE-pos;i++) { S->str[i+pos-1] = T.str[i]; } S->length = MAXSIZE; return 0; } } int StrDelete(SeqString *S,int pos,int len)//串的刪除操作(在串S中刪除pos開始的len個字符,然後將後面的字符向前移動) { int i,flag; if(pos < 0 || len < 0 || pos+len-1 > S->length) { printf("刪除位置不對,參數len不合法\n"); flag = 0; } else { for(i = pos+len;i <= S->length-1;i++) { S->str[i-len] = S->str[i]; } S->length = S->length -len;//改動串S的長度 flag = 1; } return flag; } int StrConcat(SeqString *T,SeqString S)//串的連接操作(將串S連接在串T的後面) { int i,flag; if(T->length +S.length <= MAXSIZE) { 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 < MAXSIZE) { for(i = T->length ;i < MAXSIZE;i++) { T->str[i] = S.str[i-T->length]; } T->length = MAXSIZE; flag = 0; } return flag; } int SubString(SeqString *Sub,SeqString S,int pos,int len)//截取子串操作(截取串S中從第pos個字符開始,長度為len的連續字符,並賦值給Sub) { int i; if(pos < 0 || len < 0 || pos+len-1 > S.length) { printf("參數pos和len不合法\n"); 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)//串的定位操作(在主串S中的第pos個位置開始查找子串T,假設主串S中存在與串T值相等的子串。返回子串在主串第pos個字符後第一次出現的位置) { 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//假設當前相應位置的字符不相等,則從串S的下一個字符開始。從T的第0個字符開始比較 { i = i-j+1; j = 0; } } if(j >= T.length)//假設在串S中找到串T,則返回子串T在主串S中的位置 { return i-j+1; } else { return -1; } } int StrReplace(SeqString *S,SeqString T,SeqString V)//串的替換操作(假設串S中存在子串T。則用V替換串S中的全部子串T) { int flag; int i = 0; if(StrEmpty(T)) { return 0; } while(i) { i = StrIndex(*S,i,T);//利用串的定位操作在串S中查找T的位置 if(i) { StrDelete(S,i,StrLength(T));//假設找到子串T,則將S中的串T刪除 flag = StrInsert(S,i,V);//將V插入 if(!flag) { return 0; } i += StrLength(V); } } 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"); }
執行結果:
串的應用舉例