Skip to content

Instantly share code, notes, and snippets.

@iamarkdev
Last active December 8, 2016 02:10
Show Gist options
  • Save iamarkdev/b7165dc0428b7d5a11aee53f6734e417 to your computer and use it in GitHub Desktop.
Save iamarkdev/b7165dc0428b7d5a11aee53f6734e417 to your computer and use it in GitHub Desktop.
Returns 1 if string c occurs at the end of string s
#include <stdio.h>
int mystrend(char *s, char *c) {
int sLen = 0;
int cLen = 0;
while (*s != '\0') {
s++, sLen++;
}
while (*c != '\0') {
c++, cLen++;
}
if (cLen > sLen) {
return 0;
}
while (*s == *c) {
if (cLen == 0) {
return 1;
}
s--, c--, cLen--;
}
/*
Or, we could write this while loop as a for loop and save 2 lines...
for (; *s == *c; s--, c--, cLen--) {
if (cLen == 0) {
return 1;
}
}
*/
return 0;
}
int main() {
printf("%d\n", mystrend("abcd", "bd"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment