Page 107

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 <stdbool.h>

bool strend(char *s, char *t);

int main()
{
	char *str1 = "Hello world!";
	char *str2 = "world!";
	char *str3 = "world";
	char *str4 = "England!";
	char *str5 = "";

	printf("'%s' at the end of '%s': %d\n", str2, str1, strend(str1, str2));
	printf("'%s' at the end of '%s': %d\n", str1, str1, strend(str1, str1));
	printf("'%s' at the end of '%s': %d\n", str3, str1, strend(str1, str3));
	printf("'%s' at the end of '%s': %d\n", str4, str1, strend(str1, str4));
	printf("'%s' at the end of '%s': %d\n", str5, str1, strend(str1, str5));
}

bool strend(char *s, char *t)
{
	int i;

	for (i = 0; *s != '\0'; s++) {
		if (*s == *(t + i)) {
			i++;
		} else {
			i = 0;
		}
	}

	return t[i] == '\0';
}