Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program for Naive algorithm for Pattern Searching
Pattern matching in C is the process of finding if a string (pattern) is present within another string (text). For example, the string "algorithm" is present within the string "naive algorithm". If found, its location (starting position) is displayed. The Naive algorithm is a simple brute-force approach that checks every possible position in the text.
Syntax
void naivePatternSearch(char text[], char pattern[]);
Algorithm
The Naive Pattern Searching algorithm works as follows −
naive_algorithm(pattern, text)
Input ? The text and the pattern
Output ? locations where the pattern is present in the text
Start
pat_len := pattern size
str_len := string size
for i := 0 to (str_len - pat_len), do
for j := 0 to pat_len, do
if text[i+j] ? pattern[j], then
break
if j == pat_len, then
display position i as pattern found
End
Example 1: Basic Pattern Search
Here's a simple implementation of the Naive pattern searching algorithm −
#include <stdio.h>
#include <string.h>
void naivePatternSearch(char text[], char pattern[]) {
int textLen = strlen(text);
int patternLen = strlen(pattern);
for (int i = 0; i <= textLen - patternLen; i++) {
int j;
for (j = 0; j < patternLen; j++) {
if (text[i + j] != pattern[j]) {
break;
}
}
if (j == patternLen) {
printf("Pattern found at index %d<br>", i);
}
}
}
int main() {
char text[] = "HERE IS A NICE CAP";
char pattern[] = "NICE";
printf("Text: %s<br>", text);
printf("Pattern: %s<br>", pattern);
naivePatternSearch(text, pattern);
return 0;
}
Text: HERE IS A NICE CAP Pattern: NICE Pattern found at index 10
Example 2: Multiple Pattern Occurrences
This example demonstrates finding multiple occurrences of a pattern −
#include <stdio.h>
#include <string.h>
void naivePatternSearch(char text[], char pattern[]) {
int textLen = strlen(text);
int patternLen = strlen(pattern);
int found = 0;
for (int i = 0; i <= textLen - patternLen; i++) {
int j;
for (j = 0; j < patternLen; j++) {
if (text[i + j] != pattern[j]) {
break;
}
}
if (j == patternLen) {
printf("Pattern found at index %d<br>", i);
found = 1;
}
}
if (!found) {
printf("Pattern not found<br>");
}
}
int main() {
char text[] = "XYZXACAADXYZXYZX";
char pattern[] = "XYZX";
printf("Text: %s<br>", text);
printf("Pattern: %s<br>", pattern);
naivePatternSearch(text, pattern);
return 0;
}
Text: XYZXACAADXYZXYZX Pattern: XYZX Pattern found at index 0 Pattern found at index 9 Pattern found at index 12
Time Complexity
The time complexity of the Naive Pattern Searching algorithm is O(m×n), where:
- m is the length of the pattern
- n is the length of the text
In the worst case, we might need to check every character of the pattern against every position in the text.
Key Points
- Simple to understand and implement
- Works well for small texts and patterns
- No preprocessing required
- Inefficient for large texts due to O(m×n) complexity
- Can find all occurrences of a pattern in the text
Conclusion
The Naive pattern searching algorithm is a straightforward approach for finding patterns in text. While not the most efficient for large datasets, it serves as an excellent foundation for understanding more advanced pattern matching algorithms like KMP or Boyer-Moore.
