Created
April 18, 2015 11:10
-
-
Save tomcha/4392fb5f1f271a271c95 to your computer and use it in GitHub Desktop.
2-5
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
| #include <stdio.h> | |
| int any(char s1[], char s2[]); | |
| int main(){ | |
| //"abcdefg" | |
| //"cde" -> 2 | |
| //"cdf" -> -1 | |
| printf("1:%d\n", any("abcdef", "cde")); | |
| printf("2:%d\n", any("abcdef", "cdf")); | |
| } | |
| int any(char s1[], char s2[]){ | |
| int s2size; | |
| for(s2size = 0; s2[s2size] != '\0' ; s2size++){ | |
| } | |
| int i, j; | |
| int matchedsize = 0; | |
| for(i = 0, j = 0 ; s1[i] != '\0'; i++){ | |
| if(s1[i] == s2[j]){ | |
| // printf("a:%c%c:%d\n", s1[i], s2[j],i); | |
| matchedsize++; | |
| j++; | |
| if(matchedsize == s2size){ | |
| return i - (s2size - 1); | |
| } | |
| }else{ | |
| matchedsize = 0; | |
| j = 0; | |
| } | |
| } | |
| return -1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment