Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #6726

Re: K&R2, exercise 5-4, strend(s,t)

From Mark Bluemel <mark_bluemel@pobox.com>
Newsgroups comp.lang.c
Subject Re: K&R2, exercise 5-4, strend(s,t)
Date 2011-06-23 14:08 +0100
Organization A noiseless patient Spider
Message-ID <itvds4$nr9$1@dont-email.me> (permalink)
References <4e031bce$0$312$14726298@news.sunsite.dk>

Show all headers | View raw


On 06/23/2011 11:56 AM, arnuld wrote:
> K&r2, exercise 5-4, page 107
>
> Write the functions strend(s,t), which returns 1 if the string t occurs
> at the end of the string s, and zero otherwise.

Your solution presupposes strlen(), so it may as well presuppose 
strcmp(). If you don't already have them, write them as building blocks 
for this...

As others have suggested one testcase doesn't prove much - even a 
stopped clock is right twice a day.

Here's something I threw together.

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

int strend(const char* s1, const char* s2);

int main(void)
{
   const char* arr1[]  = { NULL,
                           "",
                           "comp.lang.c",
                           "comp.lang.perl",
                           "uk.fan.monkees"};
   const char* arr2[]  = { NULL,
                           "",
                          "comp.lang.",
                          "unutterablyLongString",
                          "lang.c",
                          "ees",
                          "l"
                         };
   int i;
   int j;

   for (i = 0 ; i < (sizeof arr1/sizeof (char *)); i++) {
     for (j = 0 ; j < (sizeof arr2/sizeof (char *)); j++) {
       printf("strend(%s, %s) = %d\n", arr1[i], arr2[j], strend(arr1[i], 
arr2[j]));
     }
   }

   return 0;
}


/* return 1 if str2 occurs at the end of str1, zero otherwise */
  int strend(const char* str1, const char* str2)
   {
     int str1_len;
     int str2_len;
     const char *comparison_start;

     if ((str1 == NULL) || (str2 == NULL)) {
       return 0;
     }

     str1_len = strlen(str1);
     str2_len = strlen(str2);

     if (str2_len  == 0) {
       return 0;
     }

     if (str2_len > str1_len) {
       return 0;
     }

     comparison_start = str1 + str1_len - str2_len;
     return !strcmp(comparison_start,str2);
   }

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

K&R2, exercise 5-4, strend(s,t) arnuld <sunrise@invalid.address> - 2011-06-23 10:56 +0000
  Re: K&R2, exercise 5-4, strend(s,t) Ian Collins <ian-news@hotmail.com> - 2011-06-23 23:03 +1200
  Re: K&R2, exercise 5-4, strend(s,t) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-06-23 13:28 +0100
  Re: K&R2, exercise 5-4, strend(s,t) Mark Bluemel <mark_bluemel@pobox.com> - 2011-06-23 14:08 +0100
    Re: K&R2, exercise 5-4, strend(s,t) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-06-24 02:22 +0300
      Re: K&R2, exercise 5-4, strend(s,t) Gene <gene.ressler@gmail.com> - 2011-06-23 18:14 -0700
        Re: K&R2, exercise 5-4, strend(s,t) Ike Naar <ike@sverige.freeshell.org> - 2011-06-24 06:18 +0000
          Re: K&R2, exercise 5-4, strend(s,t) Ike Naar <ike@sverige.freeshell.org> - 2011-06-24 06:58 +0000
    Re: K&R2, exercise 5-4, strend(s,t) Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-25 10:07 -0700
      Re: K&R2, exercise 5-4, strend(s,t) arnuld <sunrise@invalid.address> - 2011-06-27 08:02 +0000
        Re: K&R2, exercise 5-4, strend(s,t) Mark Bluemel <mark_bluemel@pobox.com> - 2011-06-27 10:13 +0100
        Re: K&R2, exercise 5-4, strend(s,t) Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-27 11:54 -0700
      Re: K&R2, exercise 5-4, strend(s,t) Mark Bluemel <mark_bluemel@pobox.com> - 2011-06-27 09:07 +0100
        Re: K&R2, exercise 5-4, strend(s,t) Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-27 11:51 -0700
          Re: K&R2, exercise 5-4, strend(s,t) Mark Bluemel <mark_bluemel@pobox.com> - 2011-06-28 08:19 +0100
            Re: K&R2, exercise 5-4, strend(s,t) Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-28 16:03 -0700

csiph-web