数据结构(CC++)——顺序串

    技术2025-12-30  6

    #include<stdio.h> #include<stdlib.h> #include<string.h> #define MaxSize 255 typedef struct { char ch[MaxSize]; int length; }String; void StrAssign(String& S,char a[]) { S.length=strlen(a); for (int i = 0; i < S.length; i++) { S.ch[i] = a[i]; } } void StrPrint(String S) { for (int i = 0; i < S.length; i++) { printf("%c", S.ch[i]); } } void StrCopy(String& S, String T) { S = T; /*for (int i = 0; i < T.length; i++) { S.ch[i] = T.ch[i]; } S.length = T.length; */ } bool StrEmpty(String S) { if (S.length == 0) return true; return false; } int StrCompare(String S, String C) { if (S.length != C.length) return S.length - C.length; for (int i = 0; i < S.length; i++) { if (S.ch[i] != C.ch[i]) { return S.ch[i] - C.ch[i]; } } return 0; } int index(String S, String C) { int i = 0, j = 0; while (i<S.length&&j<C.length){ if (S.ch[i] == C.ch[j]) { i++; j++; }else{ i = i - j + 1; j = 0; } } if (j == C.length) return i - j+1; return 0; } int main() { char a[] = "i love chian"; String S; StrAssign(S, a); char b[] = "chian"; String C; StrAssign(C, b); int d = index(S, C); printf("%d", d); return 1; }
    Processed: 0.013, SQL: 9