Last active
December 8, 2016 02:10
-
-
Save iamarkdev/b7165dc0428b7d5a11aee53f6734e417 to your computer and use it in GitHub Desktop.
Returns 1 if string c occurs at the end of string s
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> | |
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