1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include<stdio.h> #include<string.h> #define MAXLEN 255 typedef struct { char ch[MAXLEN]; int length; }SString; bool StrAssing(SString *T, char ch[]); bool SubString(SString *sub, SString S, int pos, int len); int StrCompare(SString S, SString T); int StrLength(SString T); int Index(SString S, SString T); void get_next(SString T, int next[]); int main(void) { SString S, sub; char ch[] = "This is String Demo Code"; StrAssing(&S, ch); SubString(&sub, S, 2, 5); int index = Index(S, sub); printf("index=%d\n", index); return 0; }
bool StrAssing(SString *T, char str[]) { memset(T->ch, 0, MAXLEN); T->length = 0; strcpy(T->ch, str); T->length = (int)strlen(str); printf("len=%d\n",strlen(T->ch)); }
bool SubString(SString *sub, SString S, int pos, int len) { if (pos + len - 1 > S.length) { return false; } memset(sub->ch, 0, MAXLEN); sub->length = 0; strncpy(sub->ch, S.ch + pos, len); sub->length=len; return true; }
int StrCompare(SString S, SString T) { for (int i = 1; i <= S.length && i <= T.length; i++) { if (S.ch[i] != T.ch[i]) { return S.ch[i] - T.ch[i]; } } return S.length - T.length; }
int Index(SString S, SString T) { int i = 1, j = 1; int next[T.length + 1]; get_next(T, next); while (i <= S.length && j <= T.length) { if (j==0 || S.ch[i] == T.ch[j]) { ++i; ++j; } else { j = next[j]; } } if (j > T.length) { return i - T.length; } else { return 0; } }
void get_next(SString T, int next[]) { int i = 1, j = 0; next[1] = 0; while (i < T.length) { if (j == 0 || T.ch[i] == T.ch[j]) { ++i; ++j; next[i] = j; } else { j = next[j]; } } }
int StrLength(SString T) { return T.length; }
|