PROGRAM PENCOCOKAN STRING

#include <cstdlib>
#include <iostream>

using namespace std;
void FungsiGagal(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 pencocokan(char T[], char P[]){
    int i,j,F[100];
    int m=strlen(P);
    int n=strlen(T);
    FungsiGagal(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[1000],P[100];

cout<<"SAPTO NUGROHO "<<endl;
cout<<"11018043 "<<endl;
cout<<""<<endl;
cout<<"Masukkan Teks : ";
while(gets(T)){
cout<<"Masukkan Pattern : ";
        gets(P);
        int idx=pencocokan(T,P);
        if(idx!=-1)
        cout<<"Pattern "<<T<<" ditemukan pada karakter ke- "<<idx+1<<endl;
        else
        cout<<"Pattern "<<T<<"tidak ditemukan"<<endl; }

    system("PAUSE");
    return EXIT_SUCCESS;
}

Komentar