Pencocokan String

#include <cstdlib>
#include <iostream>

using namespace std;

void Pencocokan(char P[], int F[],int m){
int i,j;
F[0]=0;
j=0; i=1;
while(i<m){
if(P[i]==P[j]){
F[i]=j+1;
i++;
j++;
}else if(j>0){
j=F[j-1];
}else {
F[i]=0;
i++; } }
}

int StringMatch(char T[], char P[]){
int i,j,F[100];
int m=strlen(P);
int n=strlen(T);
Pencocokan(P,F,m);
i=0; j=0;
while(i<n){
if(T[i]==P[j]){
if(j==m-1)
return i-j;
else{
i++;
j++; }
}else if(j>0){
j=F[j-1];
}else{
i++; } }
return -1; }

int main(int argc, char *argv[])
{

char T[200],P[100];
cout<<“Masukan Teks : “;
while(gets(T)){
cout<<“Masukan Pattern : “;
gets(P);
int idx=StringMatch(T,P);
if(idx!=-1)
cout<<“String ditemukan pada index ke- “<<idx<<endl;
else
cout<<“Pattern Tidak Ditemukan”<<endl; }

system(“PAUSE”);
return EXIT_SUCCESS;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s