How to Match a Pattern in a String in C? Last Updated : 15 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Matching a pattern in a string involves searching for a specific sequence of characters within a larger string. In this article, we will learn different methods to efficiently match patterns in a string in C.The most straightforward method to match a string is by using strstr() function. Let’s take a look at an example: C #include <stdio.h> #include <string.h> int main() { char s[] = "hello geeks"; char p[] = "geeks"; // Using strstr to find first occurrence // of the pattern p in s char *res = strstr(s, p); if (res != NULL) printf("Found at position: %ld", res - s); else printf("Not found."); return 0; } OutputFound at position: 6Explanation: The strstr() function in C is used to find the first occurrence of a substring (pattern) within a larger string. It returns a pointer to the first occurrence of the pattern in the string, or NULL if the pattern is not found. The function performs a case-sensitive search, meaning it will only match the exact characters as specified in the pattern.There are also a few other methods to match a pattern in a string in C. Some of them are as follows:Using Regular ExpressionsWe can use the regcomp() function to compile a regular expression pattern and regexec() to search for it in the given string. If the pattern matches the string, the function returns 1, otherwise, it returns 0. C #include <stdio.h> #include <string.h> #include <regex.h> int search(char *s, char *p) { regex_t regex; int ret; // Compile the regular expression ret = regcomp(®ex, p, 0); // Execute the regular expression match ret = regexec(®ex, s, 0, NULL, 0); regfree(®ex); // Return 1 if match found, 0 otherwise return ret == 0; } int main() { char s[] = "hello geeks"; char p[] = "geeks"; // Matching pattern p in string s int res = search(s, p); if (res) printf("Found"); else printf("Not found."); return 0; } OutputFoundUsing a LoopIn this method, we iterate through the string and compare each character in the string with the pattern. If we find a match, we return the position of the match. C #include <stdio.h> int search(char *s, char *p) { int i = 0, j = 0; // Traverse the string s while (s[i] != '\0') { // Match the characters of p one by one if (s[i] == p[j]) { j++; // If all characters are matched if (p[j] == '\0') return 1; } else j = 0; i++; } // If pattern is not found return 0; } int main() { char s[] = "hello geeks"; char p[] = "geeks"; // Matching pattern p in string s int res = search(s, p); if (res) printf("Found"); else printf("Not found."); return 0; } OutputFound Comment More infoAdvertise with us Next Article How to Match a Pattern in a String in C? A anjalibo6rb0 Follow Improve Article Tags : C Programs C Language C-String C Examples Similar Reads How to Search in Array of Struct in C? In C, a struct (short for structure) is a user-defined data type that allows us to combine data items of different kinds. An array of structs is an array in which each element is of struct type. In this article, we will learn how to search for a specific element in an array of structs. Example: Inpu 2 min read How to Split a String by a Delimiter in C? Splitting a string by a delimiter is a common task in programming. In C, strings are arrays of characters, and there are several ways to split them based on a delimiter. In this article, we will explore different methods to split a string by a delimiter in C. Splitting Strings in CThe strtok() metho 2 min read How to Find Length of a String Without string.h and Loop in C? C language provides the library for common string operations including string length. Otherwise, a loop can be used to count string length. But is there other ways to find the string length in C apart from above two methods. In this article, we will learn how to find length of a string without strin 2 min read How to Split a String by Multiple Delimiters in C? In C, strings are arrays of characters using string manipulation often requires splitting a string into substrings based on multiple delimiters. In this article, we will learn how to split a string by multiple delimiters in C.ExampleInput:char inputString[] = "Hello,World;This|is.GeeksForGeeks";char 2 min read C program to print name pattern The printing of patterns is the most common and interesting problem. This C program prompts users to input their names and transform each letter of the name into a visually appealing big star pattern. For Example, Input: NAME Output: * * **** * * ****** ** * * * ** ** * * * * ****** * ** * **** * ** 5 min read How to Read a File Line by Line in C? In C, reading a file line by line is a process that involves opening the file, reading its contents line by line until the end of the file is reached, processing each line as needed, and then closing the file.Reading a File Line by Line in CReading a file line by line is a step by step:1. Opening th 3 min read C Program to Print the Length of a String using Pointers C language provides a built-in function strlen() but in this article, we will learn how to find and print the length of the string using pointers in C.The easiest method to find the length of a string using pointers is by calculating difference between the pointers to the first and last character of 2 min read How to Create a Dynamic Array of Strings in C? In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the progra 3 min read How to Read Data Using sscanf() in C? In C, sscanf() function stands for "String Scan Formatted". This function is used for parsing the formatted strings, unlike scanf() that reads from the input stream, the sscanf() function extracts data from a string. In this article, we will learn how to read data using sscanf() function in C. Examp 2 min read Lex Program to Find if a Character Apart from Alphabet Occurs in a String Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The commands for executing the lex program are: lex abc.l (abc is the file name) cc lex.yy.c ./a.out Pro 3 min read Like