|
| 1 | +/* |
| 2 | + * Algorithm: Naive String Matching |
| 3 | + * Description: Simple pattern matching algorithm that checks for the pattern |
| 4 | + * at every position in the text without any preprocessing. |
| 5 | + * Time Complexity: O(n * m) in worst case (n = text length, m = pattern length) |
| 6 | + * Space Complexity: O(1) |
| 7 | + * Author: tanshen-kun |
| 8 | + * |
| 9 | + * Hacktoberfest2025 |
| 10 | + */ |
| 11 | + |
| 12 | +#include <stdio.h> |
| 13 | +#include <string.h> |
| 14 | + |
| 15 | +/** |
| 16 | + * Naive string matching algorithm |
| 17 | + * Searches for all occurrences of a pattern in a given text |
| 18 | + * |
| 19 | + * @param text : string / text to search |
| 20 | + * @param pattern : pattern to search |
| 21 | + * @return : None |
| 22 | + */ |
| 23 | +void naive_search(char *text, char *pattern) { |
| 24 | + int textLength = strlen(text); |
| 25 | + int patternLength = strlen(pattern); |
| 26 | + int occurrences = 0; |
| 27 | + |
| 28 | + if (patternLength == 0 || patternLength > textLength) { |
| 29 | + printf("[x] No occurrences found.\n"); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + printf("[-] Searching for pattern %s in text %s:\n", pattern, text); |
| 34 | + |
| 35 | + for (int i = 0; i <= textLength - patternLength; i++) { |
| 36 | + int j; |
| 37 | + for (j = 0; j < patternLength; j++) { |
| 38 | + if (text[i + j] != pattern[j]) { |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (j == patternLength) { |
| 44 | + printf("[*] Pattern found at index %d\n", i); |
| 45 | + occurrences++; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if (occurrences == 0) { |
| 50 | + printf("[x] Pattern not found in text.\n"); |
| 51 | + } else { |
| 52 | + printf("[*]Total occurrences: %d\n", occurrences); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Test function with multiple test cases |
| 58 | + */ |
| 59 | +void test_naive_algorithm() { |
| 60 | + |
| 61 | + // Test case 1: Simple match |
| 62 | + printf("[-] Test Case 1 - Simple Pattern Matching:\n"); |
| 63 | + char text1[] = "ABAAABCDABABCABCABCDAB"; |
| 64 | + char pattern1[] = "ABCAB"; |
| 65 | + naive_search(text1, pattern1); |
| 66 | + printf("\n"); |
| 67 | + |
| 68 | + // Test case 2: Multiple occurrences |
| 69 | + printf("[-] Test Case 2 - Multiple Occurrences:\n"); |
| 70 | + char text2[] = "ABABCABABA"; |
| 71 | + char pattern2[] = "ABA"; |
| 72 | + naive_search(text2, pattern2); |
| 73 | + printf("\n"); |
| 74 | + |
| 75 | + // Test case 3: Pattern not found |
| 76 | + printf("[-] Test Case 3 - Pattern Not Found:\n"); |
| 77 | + char text3[] = "ABCDEFGHIJK"; |
| 78 | + char pattern3[] = "XYZ"; |
| 79 | + naive_search(text3, pattern3); |
| 80 | + printf("\n"); |
| 81 | + |
| 82 | + // Test case 4: Pattern at beginning |
| 83 | + printf("[-] Test Case 4 - Pattern at Beginning:\n"); |
| 84 | + char text4[] = "HELLO WORLD"; |
| 85 | + char pattern4[] = "HELLO"; |
| 86 | + naive_search(text4, pattern4); |
| 87 | + printf("\n"); |
| 88 | + |
| 89 | + // Test case 5: Pattern at end |
| 90 | + printf("[-] Test Case 5 - Pattern at End:\n"); |
| 91 | + char text5[] = "PROGRAMMING"; |
| 92 | + char pattern5[] = "ING"; |
| 93 | + naive_search(text5, pattern5); |
| 94 | + printf("\n"); |
| 95 | +} |
| 96 | + |
| 97 | +int main() { |
| 98 | + printf("[--- Naive String Matching Algorithm in C ---]\n\n"); |
| 99 | + |
| 100 | + test_naive_algorithm(); |
| 101 | + |
| 102 | + printf("Algorithm Information :\n"); |
| 103 | + printf("=> Time Complexity: O(n * m)\n"); |
| 104 | + printf("=> Space Complexity: O(1)\n"); |
| 105 | + printf("=> No preprocessing or optimization\n"); |
| 106 | + printf("=> Useful for small strings\n"); |
| 107 | + printf("=> Inefficient for large inputs or repetitive patterns\n"); |
| 108 | + |
| 109 | + return 0; |
| 110 | +} |
0 commit comments