Java Program for frequency of a substring in a string



Java Program for frequency of a substring in a string

Sample Code

import java.util.*;
class frequency_sub_string {
  static public void main(String[] args) {
    String txt = "WikiTechy";
    String pat = "ech";
    int M = pat.length();
    int N = txt.length();
    int res = 0;
    for (int i = 0; i <= N - M; i++) {
      int j;
      for (j = 0; j < M; j++) {
        if (txt.charAt(i + j) != pat.charAt(j)) {
          break;
        }
      }
      if (j == M) {
        res++;
        j = 0;
      }
    }
    System.out.println(res);
  }
}

Output

1

Related Searches to Java Program for frequency of a substring in a string