Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created April 18, 2015 11:10
Show Gist options
  • Select an option

  • Save tomcha/4392fb5f1f271a271c95 to your computer and use it in GitHub Desktop.

Select an option

Save tomcha/4392fb5f1f271a271c95 to your computer and use it in GitHub Desktop.
2-5
#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