Write a Modular C Programming code to implement BFSS or Brute force string Search DSA
Input − The text and the pattern
Output − locations, where the pattern is present in the text
CODE:
#include<stdio.h> #include<string.h> // Function to search for a given pattern in a given text using brute-force algorithm void bruteForceStringSearch(char* text, char* pattern) { int m = strlen(text); // length of text int n = strlen(pattern); // length of pattern int i, j; for (i = 0; i <= m - n; i++) { // Check for pattern match starting at position i for (j = 0; j < n; j++) { if (text[i + j] != pattern[j]) break; // mismatch detected } if (j == n) // pattern found at position i printf("Pattern found at position %d\n", i); } } int main() { char text[] = "AABAACAADAABAABA"; char pattern[] = "AABA"; printf("\n\nText: %s\n", text); printf("Pattern: %s\n", pattern); bruteForceStringSearch(text, pattern); return 0; }
OUTPUT
Text: AABAACAADAABAABA Pattern: AABA Pattern found at position 0 Pattern found at position 9 Pattern found at position 12 Process returned 0 (0x0) execution time : 0.036 s Press any key to continue.
Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:
Top 100+ C Programming codes – KLE Technological University