P88 例4-3 编写一个程序,测试BFIndex()函数的正确性。

P88 例4-3 编写一个程序,测试BFIndex()函数的正确性。P88 例 4 3 编写一个程序 测试 BFIndex 函数的正确性 头文件 DString h include stdio h include stdlib h include string h typedef struct char str int string h stdlib h stdio h

大家好,我是讯享网,很高兴认识大家。

P88 例4-3 编写一个程序,测试BFIndex()函数的正确性。

头文件:DString.h


讯享网

#include<stdio.h> #include<stdlib.h>  #include<string.h> typedef struct { 
    char* str; int maxLength; int length; }DString; void Initiate(DString* S, int max, char* string) { 
    int i; int size = max + 1; S->str = (char*)malloc(sizeof(char) * size); if (S->str != NULL) { 
    S->maxLength = max; S->length = (int)strlen(string); for (i = 0; i < S->length; i++) S->str[i] = string[i]; S->str[i] = '\0'; } } int Insert(DString* S, int pos, DString T) { 
    if (pos < 0) { 
    printf("参数pos出错!"); return 0; } else { 
    int i; char* buffer = (char*)malloc(sizeof(char) * (S->length + 1)); buffer[S->length] = '\0'; for (i = 0; i < S->length; i++) { 
    buffer[i] = S->str[i]; } if (S->length + T.length > S->maxLength) { 
    free(S->str); int size = S->length + T.length + 1; S->str = (char*)malloc(sizeof(char) * size); S->str[size - 1] = '\0'; S->maxLength = S->length + T.length; } for (i = S->length - 1; i >= pos; i--) S->str[i + T.length] = buffer[i]; for (i = 0; i < T.length; i++) S->str[pos + i] = T.str[i]; S->length += T.length; S->str[S->length + T.length] = '\0'; free(buffer); return 1; } } int Delete(DString* S, int pos, int len) { 
    int i; if (S->length <= 0) { 
    printf("数组中未存放字符无元素可删!\n"); return 0; } else if (pos < 0 || len<0 || pos + len>S->length) { 
    printf("参数pos和len不合法!\n"); 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 SubString(DString* S, int pos, int len, DString* T) { 
    int i; if (pos < 0 || len<0 || pos + len>S->length) { 
    printf("参数pos和len出错!\n"); return 0; } if (len > T->maxLength) { 
    T->str = (char*)malloc((len + 1) * sizeof(char)); T->maxLength = len; } for (i = 0; i < len; i++) T->str[i] = S->str[pos + i]; T->length = len; T->str[i] = '\0'; return 1; } void Destroy(DString* S) { 
    free(S->str); S->maxLength = 0; S->length = 0; } 

讯享网

源文件:例4-3.c

讯享网#include"DString.h" int BFIndex(DString S,int start,DString T) { 
    int i=start,j=0,v; 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) v=i-T.length; else v=-1; return v; } int main() { 
    DString myString1,myString2; int max1=29,max2=9; int pos=0; Initiate(&myString1,max1,"Dada Structure Data Structure"); Initiate(&myString2,max2,"Structure"); pos=BFIndex(myString1,pos,myString2); printf("第一次查找时 pos=%d\n",pos); pos=BFIndex(myString1,pos+1,myString2); printf("第二次查找时 pos=%d\n",pos); Destroy(&myString1); Destroy(&myString2); return 0; } 
小讯
上一篇 2025-01-26 15:53
下一篇 2025-03-20 19:03

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/87251.html