以下是为您编写的 C 语言代码示例: ```c #include <stdio.h> #include <string.h> // 定位子串函数 int index(char s[], char t[]) { int i, j, pos; for (pos = 0; s[pos]!= '\0'; pos++) { // 从主串的每个位置开始比较 i = pos; j = 0; while (s[i]!= '\0' && t[j]!= '\0' && s[i] == t[j]) { // 逐个字符比较 i++; j++; } if (t[j] == '\0') { // 如果模式串匹配完毕 return pos + 1; // 返回匹配位置(从 1 开始计数) } } return 0; // 未匹配返回 0 } int main() { char s[101], t[101]; int cases; scanf("%d", &cases); while (cases--) { scanf("%s %s", s, t); int res = index(s, t); printf("比较的字符序列: "); int i, j, pos; for (pos = 0; s[pos]!= '\0'; pos++) { i = pos; j = 0; while (s[i]!= '\0' && t[j]!= '\0' && s[i] == t[j]) { printf("%c", s[i]); i++; j++; } printf(" "); } printf("\n"); printf("匹配位置: %d\n", res); } return 0; } ``` 额外需要注意的逻辑点: 1. 要注意处理输入字符串为空的情况。 2. 对于字符比较时的边界情况要仔细处理,避免越界访问。 3. 注意内存的合理使用,避免内存泄漏。 [2024-12-15 00:52:00 | AI写代码神器 | 462点数解答]