Last active
December 22, 2015 23:59
-
-
Save Embedded-linux/6550490 to your computer and use it in GitHub Desktop.
String Match In C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Program is to find the "String matching pattern from source string". | |
| #include <stdio.h> | |
| #include <string.h> | |
| int match(char[],char[]); | |
| int main() | |
| { | |
| char a[100],b[100]; | |
| int position; | |
| printf("Enter some text\n"); | |
| gets(a); | |
| printf("Enter the pattern to find in the string\n"); | |
| gets(b); | |
| position = match(a,b); | |
| if (position !=-1) | |
| { | |
| printf("pos.found at location %d\n:",position+1); | |
| } | |
| else | |
| { | |
| printf("Not found\n"); | |
| } | |
| return 0; | |
| } | |
| int match(char text[],char pattern[]) | |
| { | |
| int c,d,e, text_length,pattern_length,position = -1; | |
| text_length = strlen(text); | |
| pattern_length = strlen(pattern); | |
| if (pattern_length > text_length) | |
| { | |
| return -1; | |
| } | |
| for(c=0;c<=text_length-pattern_length;c++) | |
| { | |
| position = e = c; | |
| } | |
| for(d = 0;d < pattern_length;d++) | |
| { | |
| if(pattern[d]==text[e]) | |
| { | |
| e++; | |
| } | |
| else | |
| break; | |
| } | |
| if(d==pattern_length) | |
| { | |
| return position; | |
| } | |
| return -1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment