Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@barthr
Created June 4, 2017 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barthr/b2f986e9cb853cc0652fc6a8c4268b35 to your computer and use it in GitHub Desktop.
Save barthr/b2f986e9cb853cc0652fc6a8c4268b35 to your computer and use it in GitHub Desktop.
Exercise 4.1 from K&R C book
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment