当前位置 : 主页 > 网络编程 > 其它编程 >

字符串(C语言实现)——BF算法(暴力匹配)

来源:互联网 收集:自由互联 发布时间:2023-07-02
#include#include#include#includetypedefstruct #include#include#include#includetypedef struct{char * data;//字符指针数组存储字符串 int len;//记录字符串字符个数 }String; String* Init_String(); // 初始化字符串结构体
#include#include#include#includetypedefstruct

#include#include#include#includetypedef struct{char * data;//字符指针数组存储字符串 int len;//记录字符串字符个数 }String; String* Init_String(); // 初始化字符串结构体 void Assign_String(String * S, char * T); // 串赋值bool Strcat_String(String * S, char * T); // 串连接 char* Strsub_String(String * S, int pos, int len); // 求子串 int Strcmp_String(String * S, char * T); // 串比较 int Strindex_String(String * S, char * T); // 子串定位 bool Insert__String(String * S, char * T, int pos);// 串插入bool Delete_String(String * S, int pos, int len); // 串删除int BF_Match(String * S, char * T); // 模式匹配返回串T在串S中的位置 int Length_String(char * T); // 求串长度bool Empty_String(char * T); // 判串是否为空 void Print_String(char * T); // 打印字符串 int main(){String * S Init_String();Assign_String(S, "Hello.");printf("字符串S赋值成功\n"); printf("字符串S");Print_String(S->data);printf("\n\n");if(Strcat_String(S, " How are you today?")) {printf("字符串S连接成功\n");printf("字符串S");Print_String(S->data);printf("\n\n");}elseprintf("字符串S连接失败\n");char * T1 Strsub_String(S, 8, 3);printf("字符串S的子串");Print_String(T1);printf("\n");printf("字符串S");Print_String(S->data);printf("\n\n");char * T2 " I am fine.";int n Strcmp_String(S, T2);printf("串S vs 串T2");if(n0)printf("S T");else if(n1)printf("S > T");else if(n-1)printf("S data);printf("\n\n");char * T3 "today";printf("串T3在串S的第%d个位置\n", Strindex_String(S, T3));printf("字符串T3");Print_String(T3);printf("\n");printf("字符串S");Print_String(S->data);printf("\n\n");char * T4 " I am fine.";if(Insert__String(S, T4, 25))printf("插入成功\n");elseprintf("插入失败\n");printf("字符串T4");Print_String(T4);printf("\n");printf("字符串S");Print_String(S->data);printf("\n\n");if(Delete_String(S, 1, 7))printf("删除成功\n");elseprintf("删除失败\n");printf("字符串S");Print_String(S->data);printf("\n\n"); char * T5 "today" ; int pos BF_Match(S, T5);printf("串T5在串S中的位置%d\n",pos);printf("字符串T5");Print_String(T5);printf("\n");printf("字符串S");Print_String(S->data);printf("\n\n"); return 0;}String* Init_String(){String * S (String*)malloc(sizeof(String));//为一个字符串结构体分配存储空间 if(!S)exit(-1); S->data NULL;S->len 0;return S;}void Assign_String(String * S, char * T){if(S->data)free(S->data);/*如果原字符串有内容需要清空才能进行赋值*/int len Length_String(T);if(len 0)//判断赋值的字符串是否是空串{S->data NULL;S->len 0;return;}else{int i; S->data (char*)malloc(sizeof(char)*(len1));// 为存储字符串的字符指针数组分配存储空间for(i0; idata[i] *T;}S->len len;return;}} bool Strcat_String(String * S, char * T){if(Empty_String(S->data))return true;int i;int len Length_String(T);if(len 0){return false;}else{int n S->len;S->len len;S->data (char*)realloc(S->data,sizeof(char)*(S->len1));// 扩充字符指针数组空间 for(i0; T[i]!\0; i){S->data[n] T[i];}S->data[n] \0;return true;}}char* Strsub_String(String * S, int pos, int len){if(Empty_String(S->data))return NULL;if(len > S->len)return NULL;int i,j;char * T (char*)malloc(sizeof(char)*len1);for(ipos-1,j0; jdata[i];}T[j] \0;return T;}int Strcmp_String(String * S, char * T){if(Empty_String(S->data))return 0;int i,j;int len Length_String(T);for(i0,j0; (ilen\0\0); i, j){if(S->data[i] T[j])continue;elsebreak;}if(S->data[i] T[j] len)return 0;else if(S->len > len)return 1;elsereturn -1; }int Strindex_String(String * S, char * T){int i;for(i0; ilen; i){if(S->data[i] *T){return i1;}}return 0;}bool Insert__String(String * S, char * T, int pos){if(Empty_String(S->data))return false;if(pos > S->len)return false;int i,j;int len Length_String(T);for(iS->len; idata[i1] S->data[i];}for(j0; jdata[i] T[j];}S->data[i] \0;S->len len;return true;}bool Delete_String(String * S, int pos, int len){if(Empty_String(S->data))return false;int i pos-1;//第pos个字符的下标 for( ; ilenlen S->data[ilen];}S->data[i] \0;return true;}int BF_Match(String * S, char * T){if(Empty_String(S->data))return;int i0,j0,start0;int len Length_String(T);while(ilen T[j]){i;j;}else{start;i i - j 1;j 0;}}if(j len){printf("Force Match is success.\n");return start1;}elseprintf("Force Match is fail.\n");}int Length_String(char * T){int len 0;while(*T){len;T;}return len;}bool Empty_String(char * T){if(Length_String(T))return false;elsereturn true;}void Print_String(char * T){if(Empty_String(T))return;int i;int len Length_String(T);for(i0; i

网友评论