Problem description: Start from the index character in string S to find the starting position of the substring that is the same as that of string S1 for the first time. Algorithm idea: Scan string S starting from the index character. When its element value is equal to the first character of S1, determine whether their subsequent element values are the same in sequence, until S1 ends. If both are the same, the system returns. Otherwise, the above process continues until the S scan is complete.
// Algorithm 1 int part (SqString & S, SqString & S1, int index) {int I = index, j = 0; while (I <S. length & j <S1.length) {if (S. data [I] = S1.data [j]) {I ++; j ++;} else {I = I-j + 1; j = 0 ;}} if (j> = S1.length) return (i-S1.length); else return-1;} // algorithm 2 int Part (SqString S, SqString S1, int index) {int I, j, k; int n = S. length; int m = S1.length; for (I = index; I <n-m; I ++) {for (j = 0, k = I; j <m & S1.data [j] = S. data [k]; j ++, k ++); if (j = m) return I;} return-1 ;}