C语言数据结构——子串在主串中的定位函数

#include
#include
#include 

#define Max 100
#define TRUE 1
#define FALSE 0 

typedef unsigned char SString[Max+1];

typedef int Status;

Status StrAssign(SString T,char *strs)  
{ // 生成一个其值等于chars的串T  
    int i;  
    T[0]=0;
    for(i=0;strs[i];i++)
    {
        T[i+1]=strs[i];
        T[0]=i+1;
    }
}  

int Index(SString S,SString T,int pos)  
{ // 返回子串T在主串S中第pos个字符之后的位置。若不存在,则函数值为0。  
    // 其中,T非空,1≤pos≤StrLength(S)。算法4.5  
    int i,j;  
    i = pos;
    j = 1; 
    while( i<=S[0] && j<=T[0] )
    {
        if(S[i]==T[j])
        {
            ++i;
            ++j;
        }
        else
        {
            i=i-j+2;
            j=1;
        }
    }
    if(j>T[0])    
        return i-T[0];  
    else  
        return 0;  
}  

int main()
{
    SString S,T;
    int m;
    char strs1[Max];
    char strs2[Max];
    printf("输入主串:");
    gets(strs1);
    printf("输入子串:");
    gets(strs2);
    StrAssign(S,strs1);
    StrAssign(T,strs2);
    m=Index(S,T,1);
    if(m)
        printf("%d\n",m);
    else
        printf("0\n");
    return 0; 
}

你可能感兴趣的:(数据结构,c语言,资产管理系统)