fork download
  1. /* strindex which returns rightmost occurance */
  2.  
  3. #include<stdio.h>
  4.  
  5. int mstrindex(char source[],char searchfor[]);
  6.  
  7. int main(void)
  8. {
  9. char line[] = "abcdedfabcde";
  10. char pattern[] = "abc";
  11.  
  12. int found;
  13.  
  14. /* It should match the a the 7th position. */
  15.  
  16. found = mstrindex(line, pattern);
  17.  
  18. printf("Found the right index: %d\n", found);
  19.  
  20. }
  21.  
  22. int mstrindex(char s[],char t[])
  23. {
  24. int i,j,k, result;
  25.  
  26. result = -1;
  27.  
  28. for(i=0;s[i]!='\0';i++)
  29. {
  30. for(j=i,k=0;t[k]!='\0' && s[j]==t[k];j++,k++)
  31. ;
  32. if(k>0 && t[k] == '\0')
  33. result = i;
  34. }
  35. return result;
  36. }
  37.  
  38.  
Success #stdin #stdout 0s 4300KB
stdin
Standard input is empty
stdout
Found the right index: 7