A high-level programming language called C is frequently used in the creation of embedded devices, operating systems, and application software. The language provides built-in functions and data types that simplify complex programming tasks. The strstr() function is one of them, and it may be used to locate a substring inside a specified string.
This article provides a thorough tutorial on how to use the strstr() function to locate a substring inside a given string.
What is a String and a Substring?
Before moving toward the process of finding the occurrence of the substring in C, let’s define a substring and a string. A substring is a group of characters that make up a string. A string is a group of characters that ends with the NULL character (whose ASCII value is zero) in the computer programming language C.
Find Occurrence of Substring in C Using strstr() Function
The strstr() function is a built-in C function that takes two arguments; the first argument is a pointer to the string to be searched, and the second argument is a pointer to the substring that is being searched. Below is the syntax for the strstr() function:
When the function is invoked, it returns either NULL if str2 cannot be found or a reference to the first instance of str2 discovered inside str1.
To illustrate the use of the strstr() function in C, consider the following code snippet:
int main() {
char str[50] = "This article is for Linuxhint.";
char substr[10] = "Linux";
char *ptr;
ptr = strstr(str, substr);
if (ptr != NULL) {
printf("The substring '%s' was found in the input string\n", substr);
} else {
printf("The substring '%s' was NOT found in the input string\n", substr);
}
return 0;
}
In this above code, we declare two-character arrays of size 50 and 10 respectively. The str array contains the input string that we want to search for the substring, and the substr array contains the substring we are searching for. Next, a pointer ptr is declared, and it will be used to hold the output of the strstr() method. If the function returns a null pointer, we print a message indicating that the substring was not found.
It is worth noting that the strstr() function is case-sensitive. That means, if the input string contains uppercase letters and the substring contains lowercase letters, the function will not match them. To perform a case-insensitive search, we can use the strcasestr() function. This function works in the same way as strstr(), but it is case-insensitive.
#include <string.h>
int main() {
char str[50] = "This article is for Linuxhint.";
char substr[10] = "linux";
char* ptr;
ptr = strcasestr(str, substr);
if (ptr != NULL) {
printf("The substring '%s' was found in the input string\n", substr);
} else {
printf("The substring '%s' was NOT found in the input string\n", substr);
}
return 0;
}
Conclusion
The strstr() is a powerful function in C programming that allows us to efficiently find occurrences of substrings within strings. By using this function, we can determine if a substring exists within a given string and perform further actions accordingly. Additionally, for case-insensitive searches, the strcasestr() function can be used as an alternative to strstr().