Facebook
From Sinatrio Bimo, 4 Years ago, written in C++.
This paste is a reply to Brute Force from Sinatrio Bimo - view diff
Embed
Download Paste or View Raw
Hits: 69
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. int match(char[], char[]); int main() {
  6. char a[100], b[100];
  7. int posisi;
  8.  
  9. printf("\n\t\t Program Untuk Menentukan Substring dari sebuah String\n"); printf("\n Masukkan String: \n\t");
  10. gets(a);
  11. printf("\n Masukkan Substring: \n\t");
  12. gets(b);
  13.  
  14.   posisi = match(a,b);
  15.   if (posisi != -1) {
  16.         printf("\n\n\t\t\t Substring ADA didalam string \n\n");
  17.   }
  18.   else {
  19.         printf("\n\n\t\t\t Substring TIDAK ADA didalam string. \n\n");
  20.   }
  21. getch();
  22.   return 0;
  23. }
  24.  
  25. int match (char text[], char pattern[]) {
  26.         int c,d,e, text_length, pattern_length, posisi = -1, x=0, y=0, z=0; text_length=strlen(text);
  27.         pattern_length=strlen(pattern);
  28.  
  29.         if (pattern_length > text_length) {
  30.       return -1;
  31.         }
  32.  
  33. for (c=0;c<text_length-1;c++) {
  34.   posisi =e =c;
  35.  
  36. for (d=0;d<pattern_length;d++){
  37.   if (pattern[d]==text[e]) {
  38.         printf("\n Di %c Memenuhi", text[e]);
  39.     e++;
  40.         x=1; y=1; z++;
  41.   } else {
  42.         printf("\n Di%c Tidak Memenuhi", text[e]); y=0;
  43.         break;
  44.   }
  45. }
  46.  
  47.   if (d==pattern_length) {
  48.         return posisi;
  49.   }
  50. }
  51.         if (x==1 && y==0){
  52.       return -1;
  53.     }
  54. }
  55.