Created
June 4, 2017 14:49
-
-
Save barthr/b2f986e9cb853cc0652fc6a8c4268b35 to your computer and use it in GitHub Desktop.
Exercise 4.1 from K&R C book
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> | |
#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