Techno Blender
Digitally Yours.

Java Program to Implement Commentz-Walter Algorithm

0 32


import java.io.*;

public class CommentzWalter {

    

    

    public static int search(String text, String pattern)

    {

        

        

        int[] failure = createFailureFunction(pattern);

  

        

        

        int i = 0;

        int j = 0;

  

        

        while (i < text.length()) {

  

            

            

            

            if (text.charAt(i) == pattern.charAt(j)) {

  

                

                

                i++;

                j++;

            }

  

            

            

            if (j == pattern.length()) {

                return i - j;

            }

  

            

            

            

            

            

            if (i < text.length()

                && text.charAt(i) != pattern.charAt(j)) {

                if (j > 0) {

                    j = failure[j - 1];

                }

                else {

                    i++;

                }

            }

        }

  

        

        

        return -1;

    }

  

    

    

    public static int[] createFailureFunction(

        String pattern)

    {

        int[] failure = new int[pattern.length()];

  

        

        

        int i = 0;

        int j = 1;

  

        

        while (j < pattern.length()) {

  

            

            

            

            if (pattern.charAt(i) == pattern.charAt(j)) {

                failure[j] = i + 1;

                i++;

                j++;

            }

            else {

  

                

                

                

                

                if (i > 0) {

                    i = failure[i - 1];

                }

                else {

                    failure[j] = 0;

                    j++;

                }

            }

        }

  

        return failure;

    }

  

    public static void main(String[] args)

    {

  

        

        String text = "Hello, World! How are you?";

  

        

        String pattern = "World";

  

        

        

        int index = search(text, pattern);

  

        

        if (index >= 0) {

            System.out.println("Pattern found at index "

                               + index);

        }

        else {

            System.out.println(

                "Pattern not found in the text string");

        }

    }

}


import java.io.*;

public class CommentzWalter {

    

    

    public static int search(String text, String pattern)

    {

        

        

        int[] failure = createFailureFunction(pattern);

  

        

        

        int i = 0;

        int j = 0;

  

        

        while (i < text.length()) {

  

            

            

            

            if (text.charAt(i) == pattern.charAt(j)) {

  

                

                

                i++;

                j++;

            }

  

            

            

            if (j == pattern.length()) {

                return i - j;

            }

  

            

            

            

            

            

            if (i < text.length()

                && text.charAt(i) != pattern.charAt(j)) {

                if (j > 0) {

                    j = failure[j - 1];

                }

                else {

                    i++;

                }

            }

        }

  

        

        

        return -1;

    }

  

    

    

    public static int[] createFailureFunction(

        String pattern)

    {

        int[] failure = new int[pattern.length()];

  

        

        

        int i = 0;

        int j = 1;

  

        

        while (j < pattern.length()) {

  

            

            

            

            if (pattern.charAt(i) == pattern.charAt(j)) {

                failure[j] = i + 1;

                i++;

                j++;

            }

            else {

  

                

                

                

                

                if (i > 0) {

                    i = failure[i - 1];

                }

                else {

                    failure[j] = 0;

                    j++;

                }

            }

        }

  

        return failure;

    }

  

    public static void main(String[] args)

    {

  

        

        String text = "Hello, World! How are you?";

  

        

        String pattern = "World";

  

        

        

        int index = search(text, pattern);

  

        

        if (index >= 0) {

            System.out.println("Pattern found at index "

                               + index);

        }

        else {

            System.out.println(

                "Pattern not found in the text string");

        }

    }

}

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment