# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
# define MAXSTRLEN 255
typedef unsigned char Sstring [MAXSTRLEN + 1];
int index (Sstring S, Sstring T) //这个写法虽然效率也不怎么高,但是写法真是有点别致。我是参考自严蔚敏视频配套讲义写的
//主要结构if...else if...else {while ... if ... else...}的整体构思很经典的!!!
{
int i = 1;
while (i <= S[0] - T[0] + 1)
{
if (S[i] != T[1])
{
++i;
printf("头不匹配, i = %d\n", i);
}
else if (S[i + T[0] - 1] != T[T[0]])
{
++i;
printf("头匹配尾不匹配, i = %d\n", i);
}
else
{
printf("头尾匹配, i = %d\n", i);
int j = 2;
int k = 1;
while (T[j] == S[i + k] && j < T[0])
{
++k;
++j;
printf("匹配到T中第%d个字符, i = %d, j = %d, k = %d\n", j, i, j, k);
}
if (j == T[0])
{
printf("匹配完成!i = %d,
模式匹配之首位匹配
最新推荐文章于 2021-05-11 15:12:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)