Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created November 26, 2009 07:38
Show Gist options
  • Select an option

  • Save shaobin0604/243308 to your computer and use it in GitHub Desktop.

Select an option

Save shaobin0604/243308 to your computer and use it in GitHub Desktop.
/*
* Exercise 5-4. Write the function strend(s,t), which returns 1 if the
* string t occurs at the end of the string s, and zero otherwise.
*/
#include <stdio.h>
#include <string.h>
int strend(char *s, char *t)
{
int slen = strlen(s);
int tlen = strlen(t);
int i, j;
for (i = slen - 1, j = tlen - 1; i >= 0 && j >=0 && *(s + i) == *(t + j); i--, j--)
;
if (j < 0)
return 1;
else
return 0;
}
int main(void)
{
char *s1 = "some really long string.";
char *s2 = "ng.";
char *s3 = "ng";
if(strend(s1, s2))
{
printf("The string (%s) has (%s) at the end.\n", s1, s2);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.\n", s1, s2);
}
if(strend(s1, s3))
{
printf("The string (%s) has (%s) at the end.\n", s1, s3);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.\n", s1, s3);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment