Introduction
Working with strings is a fundamental aspect of programming in C, where tasks such as searching for substrings within larger strings are common. In this tutorial, we will explore methods to check if a substring exists within a given string using the C programming language. We’ll cover both built-in functions and custom implementations.
Built-in Function: strstr()
The standard library function strstr()
is provided in <string.h>
and offers an efficient way to find substrings. The function searches for the first occurrence of a substring in a larger string and returns a pointer to its beginning if found, or NULL
otherwise.
Example Usage
#include <stdio.h>
#include <string.h>
int main() {
char *sentence = "this is my sample example";
char *word = "sample";
// Use strstr to find the substring
char *found = strstr(sentence, word);
if (found != NULL) {
printf("Substring '%s' found in '%s'\n", word, sentence);
} else {
printf("Substring '%s' not found in '%s'\n", word, sentence);
}
return 0;
}
Custom Implementation
If you prefer or need to implement your substring search logic (for instance, for educational purposes), here’s how you can do it using pointers and loops.
Example Using Pointers
This method involves iterating through the main string while comparing characters with those of the substring:
#include <stdio.h>
#include <string.h>
int containsSubstring(const char *str, const char *substr) {
if (strlen(str) < strlen(substr)) return 0;
for (size_t i = 0; i <= strlen(str) - strlen(substr); ++i) {
int j;
for (j = 0; j < strlen(substr); ++j) {
if (str[i + j] != substr[j]) break;
}
if (j == strlen(substr)) return 1; // Substring found
}
return 0; // Substring not found
}
int main() {
char *sentence = "this is my sample example";
char *word = "sample";
if (containsSubstring(sentence, word)) {
printf("Substring '%s' found in '%s'\n", word, sentence);
} else {
printf("Substring '%s' not found in '%s'\n", word, sentence);
}
return 0;
}
Optimized Pointer Method
This method uses a more optimized approach with pointer arithmetic to minimize comparisons:
#include <stdio.h>
#include <string.h>
int findSubstring(const char *str, const char *substr) {
if (strlen(str) < strlen(substr)) return -1;
for (const char *p = str; ; p++) {
const char *start = p;
while (*p && *p == *(substr + (p - start))) ++p;
if (*(substr + (p - start)) == '\0') return 0; // Substring found
if (!*(p)) break;
}
return -1; // Substring not found
}
int main() {
char *sentence = "this is my sample example";
char *word = "sample";
if (findSubstring(sentence, word) == 0) {
printf("Substring '%s' found in '%s'\n", word, sentence);
} else {
printf("Substring '%s' not found in '%s'\n", word, sentence);
}
return 0;
}
Considerations and Best Practices
-
Case Sensitivity: The methods described here are case-sensitive. For case-insensitive searches, convert both strings to a common case before comparison.
-
Efficiency:
strstr()
is generally the most efficient method for substring search in C due to its implementation optimizations within the standard library. -
Edge Cases: Always consider edge cases such as empty strings and very long substrings exceeding the main string’s length.
-
Safety: Ensure that your buffers are adequately sized to prevent buffer overflows, especially when using functions like
fgets()
for input.
By understanding both built-in solutions and custom implementations, you can choose the best approach based on your specific needs and constraints in C programming projects.