Friday, February 22, 2019

Sub String occurance in another string with out case sensitive Mind Tree

public class GFG {
 
    static int stringOccuranceCount(String pat, String txt) {      
        int M = pat.length();      
        int N = txt.length();      
        int res = 0;
              String pat1=pat.toUpperCase();
              String txt1=txt.toUpperCase();
           
        /* A loop to slide pat[] one by one */
        for (int i = 0; i <= N - M; i++) {
            /* For current index i, check for
        pattern match */
            int j;          
            for (j = 0; j < M; j++) {
             
                if (txt1.charAt(i + j) != pat1.charAt(j)) {
                 
                    break;
                }
            }
             // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
            if (j == M) {              
                res++;              
                j = 0;              
            }          
        }      
        return res;      
    } 

No comments:

Post a Comment