匹配字符串|数据结构

发布于 2021-07-10  24 次阅读


//匹配字符串
#include<string.h>
#include<conio.h>
#include<stdio.h>
#define MAX 80

void Index(char* S, char* T)
{
    int i = 0, j = 0,z = 0,m;
    int Count[MAX];
    int Slen, Tlen;
    Slen = strlen(S);
    Tlen = strlen(T);
    while (i<Slen)
    {
        if (S[i]==T[j]&&j<Tlen)
        {
            i++;
            j++;
        }
        if (j==Tlen)
        {
            Count[z] = i - Tlen;
            z++;
            j = 0;
        }
        else if (S[i]!=T[j])
        {
            i = i - j + 1;
            j = 0;
        }
    }

    if (z>0)
    {
        printf("T of S is: ");
        for (m=0;m<z;m++)
        {
            printf("%-3d", Count[m]);
        }
    }
    else
    {
        printf("NO find \n");
    }

}