This should be a program to implement the Boyer-Moore algorithm for string search in C.
The text is in file data 5.txt, which has 44049 lines of strings. A search patterns includes the 52 upper and lower case letters only. Search is case-sensitive. When a program is executed, it reads in the text, prompts for a pattern, finds all the occurrences of the pattern in the text, and reports the total number of occurrences found. Don’t remove any symbols (characters) from the text. A program is required to report the number of pattern shifts and running time for each search.
Here is a sample of the text file:
The information published in this Undergraduate Calendar outlines the rules, curricula, programs and fees for the 2016-2017 academicyear, including the Semester 2016, the Fall Semester 2016 and the Winter Semester 2017. For your the Undergraduate Calendar is available in PDF format. If you wish to link to Undergraduate Calendar please refer to the Linking Guidelines. The University a full member of: The Association of Universities and Colleges of Canada...
Algorithm explanation:
1. Input the text from a file
2. Input pattern from user
3. check for mismatch in pattern from text, this checking should be done from the end of the pattern aligned to the text from the front
4. if a mismatched character is found, look for the last occurrence of the character in the pattern and shift the pattern to align that character with mismatched character in the text.
5. if the mismatched character not found, shift the pattern to the next character in the text.
6. if a pattern match is found, increment number of occurrences and search again starting from the next character in line
//***********************************************START OF PROGRAM*************************************************************
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <time.h> //to use clock() and CLOCKS_PER_SECOND
# define NO_OF_CHARS 256
// to get maximum of two integers
int maximum (int a, int b) { return (a > b)? a: b; }
// function for Boyer Moore's bad character heuristic
void badCharHeur( char *str, int size,
int badchar[NO_OF_CHARS])
{
int i;
// Initialize all occurrences as -1
for (i = 0; i < NO_OF_CHARS; i++)
badchar[i] = -1;
// Fill the actual value of last occurrence
// of a character
for (i = 0; i < size; i++)
badchar[(int) str[i]] = i;
}
//the search function
int search( char *text, char *pattern)
{
int m = strlen(pattern);
int n = strlen(text);
int occ = 0;
int badchar[NO_OF_CHARS];
/* Fill the bad character array by calling
the preprocessing function badCharHeur()
for given pattern */
badCharHeur(pattern, m, badchar);
int s = 0; // s is shift of the pattern wrt text
while(s <= (n - m))
{
int j = m-1;
while(j >= 0 && pattern[j] == text[s+j])
j--;
/* If the pattern is present at current
shift, then index j will become -1 after
the above loop */
if (j < 0)
{
occ++;
/* the pattern is shifted so that the next
character in text aligns with the last
occurrence of it in pattern.
*/
s += (s+m < n)? m-badchar[text[s+m]] : 1;
}
else
/* Shift the pattern so that the bad character
in text aligns with the last occurrence of
it in pattern. The max function is used to
make sure that we get a positive shift.
*/
s += maximum(1, j - badchar[text[s+j]]);
}
return occ;
}
int main()
{
clock_t start, end;
double exec_time;
char txt[1000000];
char pat[10000];
FILE *fp;
fp = fopen("data_5.txt", "r"); //note: the txt file should not
have any new lines in it
if(fp == NULL)
printf("\nCould not open file");
else{
printf("\nEnter the pattern to be matched: ");
gets(pat);
if(fgets(txt, 1000000, fp) != NULL){
start = clock();
printf("\nNo. of occurences: %d", search(txt, pat));
end = clock();
exec_time = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("\n\nTime Taken for finding match: %f", exec_time);
}
}
return 0;
}
//**********************************************************END OF
PROGRAM****************************************************
Please note: The file data_5.txt should be in the same directive as the program for the program to run properly
This should be a program to implement the Boyer-Moore algorithm for string search in C. The...
write a code on .C file
Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...
Write a c++ program in that file to perform a “Search and Replace All” operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they don’t read the command line information in the course document titled “Using the Compiler’s IDE”. Your program: 1. must...