Created
November 3, 2016 12:29
-
-
Save Highstaker/e6f16816b044ea1a852191e09caa7ad6 to your computer and use it in GitHub Desktop.
Looks for a pattern in string.
This file contains 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
#include <stdbool.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int findPatternRight(char* s, char* pat){ | |
bool flag = true; | |
int i, j, k, left; | |
int len_s = strlen(s); | |
int len_pat = strlen(pat); | |
for(i=len_s-1, left=len_s-len_pat;left>=0;i--, left--){ | |
flag = true; | |
for(j=left, k=0;k<len_pat;j++, k++){ | |
if(s[j] != pat[k]){ | |
flag = false; | |
break; | |
} | |
} | |
if(flag){ | |
return left; | |
} | |
} | |
return -1; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
char *in; | |
char *pat; | |
in = malloc( 10000000 * sizeof(char)); | |
pat = malloc( 10000000 * sizeof(char)); | |
printf("Enter a string to search in:"); | |
scanf("%s", in); | |
printf("Enter a pattern:"); | |
scanf("%s", pat); | |
int ind = findPatternRight(in, pat); | |
printf("%d\n", ind); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment