Javatpoint Logo
Javatpoint Logo

C++ Program to Implement Bitap Algorithm for String Matching

The Bitap Algorithm, known as the Shift-Or Algorithm, is a string-searching algorithm that efficiently performs approximate string matching. It is particularly useful for finding a pattern within a text when there might be errors or variations in the pattern. The bitmap algorithm was introduced by John Colquhoun and Gaston Gonnet in 1980.

Example:

Let us take a C++ Program to Implement the Bitap Algorithm for String Matching.

Output:

C++ Program to Implement Bitap Algorithm for String Matching

Explanation:

This program implements the Bitap Algorithm for String Matching. The variables present in the program are 't', which represents the input text, 'p' is the input pattern, 'm' represents the length of the pattern, 'p_mask' represents an array of bit masks for each character in the pattern, 'A' is a variable used for bitwise operations, 'position' represents the position where the pattern is found in the text.

The function 'bitmap_search' takes the string 't' and pattern 'p' and returns the position of the pattern in the text or -1 if not found. This function uses bit manipulation to search for the pattern in the text. It creates bit masks for the character in the pattern and then performs bitwise operations to identify the position of the pattern in the text. Another function named the find pattern function takes the string 't' as input text and string 'p' as the pattern and displays whether the pattern is found and its position. This function calls the bitmap_search function and prints the result. If the pattern is found, it displays the position otherwise and prints 'No Match'.

The main function takes two strings, representing the user's input string and pattern. After that, calls findpattern function to search for the pattern and display the result.

Example 2:

Let us take another way to implement Bitap Algorithm for String Matching in C++.

Output:

C++ Program to Implement Bitap Algorithm for String Matching

Explanation:

This program implements the Bitap Algorithm for string matching. It efficiently searches for a pattern within a text, providing approximate string-matching capabilities. This program has a Bitap class containing a constructor that accepts the patterns and calculates their length. The functions present in the class are the Preprocess function and Search functions. Preprocess function creates a bit masks for each character in the pattern, stored in the textMasks_vector. The Search function uses bitwise operations to find the pattern in the text and prints the positions where the pattern is found. The main function takes the input for the text and pattern from the user and displays the positions where the pattern is found in the text. This main function will create an instance or object of the Bitmap class with the provided pattern, ask the user for input, and call the Search function of the Bitap class to find and print the positions where the pattern is found in the text.


Next TopicComparator in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA