Skip to content

Instantly share code, notes, and snippets.

@barthr
Created June 4, 2017 14:49
Show Gist options
  • Save barthr/b2f986e9cb853cc0652fc6a8c4268b35 to your computer and use it in GitHub Desktop.
Save barthr/b2f986e9cb853cc0652fc6a8c4268b35 to your computer and use it in GitHub Desktop.

Revisions

  1. barthr created this gist Jun 4, 2017.
    35 changes: 35 additions & 0 deletions strrindex.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int strrindex(char *s, char *t);

    int main(void)
    {
    char items[] = "bartbartbart";
    char items2[] = "rt";
    printf("%d", strrindex(items, items2));
    }

    int strrindex(char *s, char *t)
    {
    int n = strlen(s);
    int d = strlen(t);
    int res = 0;

    for (int i = n - 1; i >= 0; i--)
    {
    for (int j = d - 1; j >= 0; j--)
    {
    if (s[i] == t[j])
    {
    ++res;
    }
    if (res == d)
    {
    return i;
    }
    }
    }
    return -1;
    }