#include<stdio.h>
#include<string.h>
#define W 9
#define L 30
int next[W-1];
void nextarray(char t2[W],int next2[W-1])
{
int i=0,j,k,m,n;
printf("the original string t:/n");
for(n=1;n<strlen(t2);n++)
{
printf("%c",t2[n]);
}
next2[1]=0;
next2[2]=1;
for(m=3;m<=W-2;m++)
{
if(m%2==0)
{
j=m/2;
}
if(m%2!=0)
{
j=(m+1)/2;
}
i=0;
while(j<m)
{
if(t2[i]==t2[j])
{
i++;
j++;
}
else
{
i=1;
j++;
}
}
if(j==m && i>0)
{
next2[m]=i;
}
else
{
next2[m]=1;
}
}
printf("/n");
printf("here are values for next array:/n");
for(i=1;i<W-1;i++)
{
printf("%-5d",next2[i]);
}
for(i=0;i<3;i++)
{
printf("/n");
}
}
void createstrt(char strt[W-1])
{
int i;
strt[0]='[';
printf("please input a string for t (length< %d:/n",W-1);
for(i=1;i<W-1;i++)
{
scanf("%c",&strt[i]);
}
strt[W-1]='/0';
printf("/n");
}
void createstrs(char strs[L-1])
{
int i;
strs[0]='!';
printf("please input a string for s (length< %d):/n",L-1);
for(i=1;i<L-1;i++)
{
scanf("%c",&strs[i]);
}
strs[L-1]='/0';
printf("here are the string you have input:/n");
for(i=1;i<=L-1;i++)
{
printf("%c",strs[i]);
}
printf("/n");
}
void pipei(char t2[W],char s2[L],int next[W-1])
{
int i=1,j=1;
while(i<L && j<W)
{
if(t2[j]==s2[i])
{
i++;
j++;
}
else
{
j=next[j];
if(j==0)
{
j++;
i++;
}
}
}
if(j==W-1)
{
printf("string t is locted in the [%d] of the string s/n",i);
}
else
{
printf("string t is not in the string s!/n");
}
}
int main()
{
char t[W],s[L];
clrscr();
createstrt(t);
createstrs(s);
nextarray(t,next);
pipei(t,s,next);
return 0;
}
#include<string.h>
#define W 9
#define L 30
int next[W-1];
void nextarray(char t2[W],int next2[W-1])
{
int i=0,j,k,m,n;
printf("the original string t:/n");
for(n=1;n<strlen(t2);n++)
{
printf("%c",t2[n]);
}
next2[1]=0;
next2[2]=1;
for(m=3;m<=W-2;m++)
{
if(m%2==0)
{
j=m/2;
}
if(m%2!=0)
{
j=(m+1)/2;
}
i=0;
while(j<m)
{
if(t2[i]==t2[j])
{
i++;
j++;
}
else
{
i=1;
j++;
}
}
if(j==m && i>0)
{
next2[m]=i;
}
else
{
next2[m]=1;
}
}
printf("/n");
printf("here are values for next array:/n");
for(i=1;i<W-1;i++)
{
printf("%-5d",next2[i]);
}
for(i=0;i<3;i++)
{
printf("/n");
}
}
void createstrt(char strt[W-1])
{
int i;
strt[0]='[';
printf("please input a string for t (length< %d:/n",W-1);
for(i=1;i<W-1;i++)
{
scanf("%c",&strt[i]);
}
strt[W-1]='/0';
printf("/n");
}
void createstrs(char strs[L-1])
{
int i;
strs[0]='!';
printf("please input a string for s (length< %d):/n",L-1);
for(i=1;i<L-1;i++)
{
scanf("%c",&strs[i]);
}
strs[L-1]='/0';
printf("here are the string you have input:/n");
for(i=1;i<=L-1;i++)
{
printf("%c",strs[i]);
}
printf("/n");
}
void pipei(char t2[W],char s2[L],int next[W-1])
{
int i=1,j=1;
while(i<L && j<W)
{
if(t2[j]==s2[i])
{
i++;
j++;
}
else
{
j=next[j];
if(j==0)
{
j++;
i++;
}
}
}
if(j==W-1)
{
printf("string t is locted in the [%d] of the string s/n",i);
}
else
{
printf("string t is not in the string s!/n");
}
}
int main()
{
char t[W],s[L];
clrscr();
createstrt(t);
createstrs(s);
nextarray(t,next);
pipei(t,s,next);
return 0;
}