The C Programming Language, 2nd Edition, by Kernighan and Ritchie
Exercise 5.04 on 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.

Solution by Bryan Williams

/*

    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.

    Author : Bryan Williams

*/


int strlen(char *s) /* added by RJH; source: K&R p99 */
{
  int n;

  for(n = 0; *s != '\0'; s++)
  {
    n++;
  }
  return n;
}

int strcmp(char *s, char *t) /* added by RJH; source: K&R p106 */
{
  for(;*s == *t; s++, t++)
    if(*s == '\0')
      return 0;
  return *s - *t;
}


int strend(char *s, char *t)
{
  int Result = 0;
  int s_length = 0;
  int t_length = 0;

  /* get the lengths of the strings */
  s_length = strlen(s);
  t_length = strlen(t);

  /* check if the lengths mean that the string t could fit at the string s */
  if(t_length <= s_length)
  {
    /* advance the s pointer to where the string t would have to start in string s */
    s += s_length - t_length;

    /* and make the compare using strcmp */
    if(0 == strcmp(s, t))
    {
      Result = 1;
    }
  }

  return Result;
}



#include <stdio.h>


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;
}


Category 0 Solution by Jesus Alvarez (The solution above is the correct way, this solution reverses the strings and then compares. Reversing is not necessary.)

#include <stdio.h>
#include <string.h>

#define STR_BUFFER 10000

int  strend(char *, char *);
void strrev(char *, char *);

int main(int argc, char *argv[])
{
        char string1[STR_BUFFER] = "ole.";
        char string2[STR_BUFFER] = "hole. ";

        printf ("String 1: %s\n", string1);
        printf ("String 2: %s\n", string2);

        if (strend(string1, string2)) {
                printf ("String 2 occurs at the end of string 1!\n");
        } else {
                printf ("String 2 does not occur at the end of string 1.\n");
        }
        return 0;
}

int strend(char *s, char *t)
{
        char *s_pt, *t_pt;
        char s_rev[STR_BUFFER];
        char t_rev[STR_BUFFER];

        s_pt = s_rev;
        t_pt = t_rev;

        /* Reverse the strings to make the matching more fun, but not very
         * efficient. :) */
        strrev(s, s_rev);
        strrev(t, t_rev);

        for ( ; *s_pt == *t_pt; s_pt++, t_pt++) {
                if (*s_pt == ' ' || *s_pt == '\t' || *s_pt == '\0') {
                        return 1; /* The ends of the strings match. */
                } else {
                        return 0;
                }
        }

        return 0;
}

/*
 * strrev()
 * Reverses a string.
 * @param: str  Pointer to the string to be reversed.
 * @param: dest Pointer where the result will be stored.
 */
void strrev(char *str, char *dest)
{
        int i = strlen(str)-1; /* -1 to skip the '\0' */
        for (str += i; i >=0; dest++, str--, i--) {
                /* Increment backwards over str copying to dest. */
                *dest = *str;
        }
        dest++;
        dest = '\0';
}


Category 0 Solution by Jose G. López

#include <stdio.h>

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

int main(void)
{
	char *s = "hello, hello";
	char *t = "llo";

	if (strend(s, t))
		printf("'%s' occurs at the end of '%s'\n", t, s);
	else
		printf("no occurences at the end of '%s' from '%s'\n", s, t);

	return 0;
}

int strend(char *s, char *t)
{
	int i = 0;
	int ind;

	while (*s) {
		for (ind = 0; *(s + ind) == *(t + ind) 
				&& *(s + ind) != '\0' && *(t + ind) != '\0'; ind++)
			;
		if(*(s + ind) == '\0' && *(t + ind) == '\0')
			return 1;
		s++;
	}

	return i;
}
Personal tools
Tidy_icons
not logged in