Rabu

Program Pencocokan String

procedure PencocokanString(input P : string, T :
n, m : integer,
                           output idx : integer)
{ Masukan: pattern P yang panjangnya m dan teks T yang panjangnya n. Teks T direpresentasika sebagai string (array of character)
Keluaran: lokasi awal kecocokan (idx)
}
Deklarasi
  i : integer
  ketemu : boolean

Algoritma:
  i¬0
  ketemu¬false
  while (i £ n-m) and (not ketemu) do
    j¬1
    while (j £ m) and (Pj = Ti+j ) do
      j¬j+1
    endwhile
    { j > m or Pj ¹ Ti+j }  

    if j = m then   { kecocokan string ditemukan }
      ketemu¬true
    else
      i¬i+1  {geser pattern satu karakter ke kanan teks }
    endif
  endfor
  { i > n – m or ketemu }
  if ketemu then
     idx¬i+1
  else
     idx¬-1
  endif
  

ini Programnya
 #include
#include 

using namespace std;

void stringMatch( char * Teks, int pTeks, char * Pola, int pPola )
{
  int i, j;
  i=0;
 while( i <= ( pTeks - pPola ))
  {
    j=0;
    while(j < pPola)
    {
      if ( Teks[i + j] == Pola[j] ){

        j++;
      }
      else
        break;
    }
    
    if ( j == pPola )
     cout<<" PATTERN "<<Pola<<" WAS FOUND at index : "<<i+1<<endl;

    i++;
  }
}

int main( )
{
  stringMatch( "informatics engineering", 23, "tic", 3 );

     system("PAUSE");
    return EXIT_SUCCESS;
}

source

Tidak ada komentar:

Posting Komentar