Advertisement
kamasazi99

aiz 12 lab, wzorzec

Jan 12th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. package aiznatalia12;
  2. public class Wzorzec {
  3.  
  4.     public String tekst;
  5.     public int dlugosctekstu;
  6.     public String wzorzec;
  7.     public int dlugoscwzorca;
  8.  
  9.     public Wzorzec(String t, String w) {
  10.  
  11.         tekst = t;
  12.         wzorzec = w;
  13.         dlugosctekstu = t.length();
  14.         dlugoscwzorca = w.length();
  15.  
  16.     }
  17.  
  18.     public void naiwny() {
  19.         int k = 0;
  20.         int i = 0;
  21.  
  22.         System.out.print("pozycje wystapienia wzorca: ");
  23.         while (i < dlugosctekstu - dlugoscwzorca + 1) {
  24.             int j = 0;
  25.             while (tekst.charAt(j+i) == wzorzec.charAt(j)) {
  26.                 j++;
  27.                 if (j>=wzorzec.length()) break;
  28.             }
  29.             if (j == dlugoscwzorca) {
  30.                 k++;
  31.                 System.out.print(i + ", ");
  32.                 //Write(i);
  33.             }
  34.             i++;
  35.         }
  36.         System.out.println("\b\b");
  37.         System.out.println("ilosc wystapien wzorca: " + k);
  38.     }
  39.  
  40.     private void Write(int i) {
  41.  
  42.         for (int j = 0; j < wzorzec.length(); j++) {
  43.             System.out.print(tekst.charAt(i + j));
  44.         }
  45.         System.out.println("");
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement