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


Groups > comp.lang.c > #6720 > unrolled thread

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

Started byarnuld <sunrise@invalid.address>
First post2011-06-23 10:56 +0000
Last post2011-06-28 16:03 -0700
Articles 16 — 8 participants

Back to article view | Back to comp.lang.c


Contents

  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

#6720 — K&R2, exercise 5-4, strend(s,t)

Fromarnuld <sunrise@invalid.address>
Date2011-06-23 10:56 +0000
SubjectK&R2, exercise 5-4, strend(s,t)
Message-ID<4e031bce$0$312$14726298@news.sunsite.dk>
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.


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

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


int main(void)
{
  const char* arr1 = "comp.lang.c";
  const char* arr2 = "comp.lang.";

  printf("strend(%s, %s) = %d\n", arr1, arr2, strend(arr1, arr2));

  return 0;
}


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

  /* If one of arguments is NULL, there is no pint in going next */
  if(NULL == str1 || NULL == str2)
    {
      ret = 0;
    }
  else if(strlen(str1) < strlen(str2))
    {
      ret = 0;
    }
  else if ('\0' == *str1 && '\0' == *str2)
    {
      ret = 1;
    }
  else
    {
      size_t len1 = strlen(str1);
      size_t len2 = strlen(str2);
      const char* tmp1 = str1 + len1 - 1;
      const char* tmp2 = str2 + len2 - 1;
      size_t count = 0;
      for(; *tmp1 == *tmp2 && count < len2; --tmp1, --tmp2, ++count)
	;
	
      if(count == len2 && *++tmp1 == *++tmp2) ret = 1;
      else ret = 0;
    }

  return ret;
}

==================== OUTPUT =========================
[arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra strend.c 
[arnuld@dune C]$ ./a.out 
strend(comp.lang.c, comp.lang.) = 0
[arnuld@dune C]$ 



-- 
www.lispmachine.wordpress.com
find my email-ID @above blog

[toc] | [next] | [standalone]


#6721

FromIan Collins <ian-news@hotmail.com>
Date2011-06-23 23:03 +1200
Message-ID<96gktbFmo4U2@mid.individual.net>
In reply to#6720
On 06/23/11 10:56 PM, 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.
>
>
> #include<stdio.h>
> #include<string.h>
>
> int strend(const char* s1, const char* s2);
>
>
> int main(void)
> {
>    const char* arr1 = "comp.lang.c";
>    const char* arr2 = "comp.lang.";
>
>    printf("strend(%s, %s) = %d\n", arr1, arr2, strend(arr1, arr2));

You should add test cases for the conditions you are testing as well as 
some matches.

>    return 0;
> }
>
>
> /* return 1 if str2 occurs at the end of str1, zero otherwise */
> int strend(const char* str1, const char* str2)
> {
>    int ret;
>
>    /* If one of arguments is NULL, there is no pint in going next */
>    if(NULL == str1 || NULL == str2)
>      {
>        ret = 0;
>      }
>    else if(strlen(str1)<  strlen(str2))

I would do this test last, after you assign the strlen results to len1 
and len2 to avoid calling strlen twice for each string.

>      {
>        ret = 0;
>      }
>    else if ('\0' == *str1&&  '\0' == *str2)
>      {
>        ret = 1;
>      }
>    else
>      {
>        size_t len1 = strlen(str1);
>        size_t len2 = strlen(str2);
>        const char* tmp1 = str1 + len1 - 1;
>        const char* tmp2 = str2 + len2 - 1;
>        size_t count = 0;
>        for(; *tmp1 == *tmp2&&  count<  len2; --tmp1, --tmp2, ++count)
> 	;
> 	
>        if(count == len2&&  *++tmp1 == *++tmp2) ret = 1;
>        else ret = 0;
>      }
>
>    return ret;
> }
>
> ==================== OUTPUT =========================
> [arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra strend.c
> [arnuld@dune C]$ ./a.out
> strend(comp.lang.c, comp.lang.) = 0
> [arnuld@dune C]$
>
>
>


-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#6724

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2011-06-23 13:28 +0100
Message-ID<0.4b7b846964d0b7655af9.20110623132849BST.871uykbuke.fsf@bsb.me.uk>
In reply to#6720
arnuld <sunrise@invalid.address> writes:

> 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.
<snip>
> /* return 1 if str2 occurs at the end of str1, zero otherwise */
> int strend(const char* str1, const char* str2)
> {
>   int ret;
>
>   /* If one of arguments is NULL, there is no pint in going next */
>   if(NULL == str1 || NULL == str2)
>     {
>       ret = 0;
>     }
>   else if(strlen(str1) < strlen(str2))
>     {
>       ret = 0;
>     }
>   else if ('\0' == *str1 && '\0' == *str2)
>     {
>       ret = 1;
>     }
>   else
>     {
>       size_t len1 = strlen(str1);
>       size_t len2 = strlen(str2);
>       const char* tmp1 = str1 + len1 - 1;
>       const char* tmp2 = str2 + len2 - 1;

Same error as I posted about in another thread.  When either string is
"", the tmp1 or tmp2 pointer will be invalid.

>       size_t count = 0;
>       for(; *tmp1 == *tmp2 && count < len2; --tmp1, --tmp2, ++count)
> 	;

And, you access the pointer.  In fact, you do so for all matching
strings because the decrements will always take one of the tmp pointers
to before the start of the strings.  You probably wanted test count
before you test anything else.

BTW, this is for loop abuse!  I'd write this as a while loop.

>       if(count == len2 && *++tmp1 == *++tmp2) ret = 1;
>       else ret = 0;

This looks wrong but given that the previous loops is wrong I can't
really say exactly how.  I think you intended, in the loop, to test
count first and then you'd need this extra test, but I am not sure.

I've found that "end" string processing is almost always clearer if you
point at the null:

  const char* tmp1 = str1 + len1;

and you decrement before testing a character:

  if (tmp1 > str1 && *--tmp1 == 'X') ...

etc.  However, this is a case where you should use strcmp, rather than
rolling your own loop.

>     }
>
>   return ret;
> }

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#6726

FromMark Bluemel <mark_bluemel@pobox.com>
Date2011-06-23 14:08 +0100
Message-ID<itvds4$nr9$1@dont-email.me>
In reply to#6720
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);
   }

[toc] | [prev] | [next] | [standalone]


#6771

FromPhil Carmody <thefatphil_demunged@yahoo.co.uk>
Date2011-06-24 02:22 +0300
Message-ID<87oc1o875q.fsf@bazspaz.fatphil.org>
In reply to#6726
Mark Bluemel <mark_bluemel@pobox.com> writes:
> /* return 1 if str2 occurs at the end of str1, zero otherwise */

This is pretty much what I'd do too, with a few tweaks.

>   int strend(const char* str1, const char* str2)
>    {
>      int str1_len;
>      int str2_len;

size_t is better.

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

Doesn't the empty string appear at the end of every string? Or did you mean
str1_len?

Either way, the calculation of the length you've not used yet can be 
delayed until this point, so that some trivial cases can be handled 
more quickly.

>      }
> 
>      if (str2_len > str1_len) {

If you calculate str1_len first, then you can use a strnlen[*] rather than
strlen on str2, in order to be able to bail early on the above condition.

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

Memcmp doesn't need to keep checking for '\0' characters, which you know
will be absent in the sections you're interested in comparing.

>    }

Phil

[* GNU provides one for you, but you might need to write your own,
it's a strlen with a maximum length.]

-- 
"At least you know where you are with Microsoft."
"True. I just wish I'd brought a paddle." -- Matthew Vernon

[toc] | [prev] | [next] | [standalone]


#6775

FromGene <gene.ressler@gmail.com>
Date2011-06-23 18:14 -0700
Message-ID<e4f82879-642f-4936-806c-f3a645897b76@m22g2000yqh.googlegroups.com>
In reply to#6771
On Jun 23, 7:22 pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
wrote:
> Mark Bluemel <mark_blue...@pobox.com> writes:
> > /* return 1 if str2 occurs at the end of str1, zero otherwise */
>
> This is pretty much what I'd do too, with a few tweaks.
>
> >   int strend(const char* str1, const char* str2)
> >    {
> >      int str1_len;
> >      int str2_len;
>
> size_t is better.
>
> >      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;
>
> Doesn't the empty string appear at the end of every string? Or did you mean
> str1_len?
>
> Either way, the calculation of the length you've not used yet can be
> delayed until this point, so that some trivial cases can be handled
> more quickly.
>
> >      }
>
> >      if (str2_len > str1_len) {
>
> If you calculate str1_len first, then you can use a strnlen[*] rather than
> strlen on str2, in order to be able to bail early on the above condition.
>
> >        return 0;
> >      }
>
> >      comparison_start = str1 + str1_len - str2_len;
> >      return !strcmp(comparison_start,str2);
>
> Memcmp doesn't need to keep checking for '\0' characters, which you know
> will be absent in the sections you're interested in comparing.
>
> >    }

Another approach is to keep finding the first occurrence until you
find the last:


// UNCOMPILED; UNTESTED
int strend(const char* str1, const char* str2)
{
  int len2;
  char *p, *str2loc;

  if (str1 && str2) {
    len2 = strlen(str2);
    // p points to the unsearched part of str1
    p = str1;
    for (;;) {
      char *str2loc = strstr(p, str2);
      if (!str2loc)
        return 0;
      p = str2loc + len2;
      if (*p == '\0')
        return 1;
    }
  }
  return 0;
}

[toc] | [prev] | [next] | [standalone]


#6785

FromIke Naar <ike@sverige.freeshell.org>
Date2011-06-24 06:18 +0000
Message-ID<slrn3vfsj08b2b.ab5.ike@sverige.freeshell.org>
In reply to#6775
On 2011-06-24, Gene <gene.ressler@gmail.com> wrote:
> // UNCOMPILED; UNTESTED
> int strend(const char* str1, const char* str2)
> {
>   int len2;
>   char *p, *str2loc;
>
>   if (str1 && str2) {
>     len2 = strlen(str2);
>     // p points to the unsearched part of str1
>     p = str1;
>     for (;;) {
>       char *str2loc = strstr(p, str2);
>       if (!str2loc)
>         return 0;
>       p = str2loc + len2;
>       if (*p == '\0')
>         return 1;
>     }
>   }
>   return 0;
> }

You mentioned that the code is uncompiled and untested, and in
fact it has a few compilation errors but they are easy to fix.

One "real" problem: the for loop does not terminate if str2
is the empty string.

[toc] | [prev] | [next] | [standalone]


#6786

FromIke Naar <ike@sverige.freeshell.org>
Date2011-06-24 06:58 +0000
Message-ID<slrn3vfsj08dd1.ifg.ike@sverige.freeshell.org>
In reply to#6785
On 2011-06-24, Ike Naar <ike@sverige.freeshell.org> wrote:
> On 2011-06-24, Gene <gene.ressler@gmail.com> wrote:
>> // UNCOMPILED; UNTESTED
>> int strend(const char* str1, const char* str2)
>> {
>>   int len2;
>>   char *p, *str2loc;
>>
>>   if (str1 && str2) {
>>     len2 = strlen(str2);
>>     // p points to the unsearched part of str1
>>     p = str1;
>>     for (;;) {
>>       char *str2loc = strstr(p, str2);
>>       if (!str2loc)
>>         return 0;
>>       p = str2loc + len2;
>>       if (*p == '\0')
>>         return 1;
>>     }
>>   }
>>   return 0;
>> }
>
> You mentioned that the code is uncompiled and untested, and in
> fact it has a few compilation errors but they are easy to fix.

To Gene: Sorry about the phrase "a few compilation errors"; there's
only a single compilation error in the above fragment (the assigment
``p = str1'' assigns a pointer-to-const to a pointer-to-nonconst).

There was another compilation error, but that was in the code that
I added to turn your code fragment into a complete program.

[toc] | [prev] | [next] | [standalone]


#6932

FromTim Rentsch <txr@alumni.caltech.edu>
Date2011-06-25 10:07 -0700
Message-ID<kfnoc1lalgf.fsf@x-alumni2.alumni.caltech.edu>
In reply to#6726
Mark Bluemel <mark_bluemel@pobox.com> writes:

> 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...
> [snip]
>
> /* 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);
>   }


An alternative offered for everyone's reading pleasure
and/or amusement (as each may individually determine).


 /* Return 1 if 'tail' occurs at the end of 'whole', or 0 if not. */

 int
 strend( const char *whole, const char *tail ){
    size_t w, t;

    return
       whole  &&  tail
       &&   (w = strlen( whole ))  >=  (t = strlen( tail ))
       &&   strcmp( whole+w -t, tail ) == 0
    ;
 }

[toc] | [prev] | [next] | [standalone]


#7031

Fromarnuld <sunrise@invalid.address>
Date2011-06-27 08:02 +0000
Message-ID<4e083927$0$311$14726298@news.sunsite.dk>
In reply to#6932
> On Sat, 25 Jun 2011 10:07:44 -0700, Tim Rentsch wrote:


> An alternative offered for everyone's reading pleasure and/or amusement
> (as each may individually determine).
> 
> 
>  /* Return 1 if 'tail' occurs at the end of 'whole', or 0 if not. */
> 
>  int
>  strend( const char *whole, const char *tail ){
>     size_t w, t;
> 
>     return
>        whole  &&  tail
>        &&   (w = strlen( whole ))  >=  (t = strlen( tail )) &&   strcmp(
>        whole+w -t, tail ) == 0
>     ;
>  }


Interesting :)  , if we assume not to use any of those standard string 
library functions (strcmp, strlen etc) then can we accomplish ?




-- 
www.lispmachine.wordpress.com
find my email-ID @above blog

[toc] | [prev] | [next] | [standalone]


#7038

FromMark Bluemel <mark_bluemel@pobox.com>
Date2011-06-27 10:13 +0100
Message-ID<iu9hjn$icd$1@dont-email.me>
In reply to#7031
On 06/27/2011 09:02 AM, arnuld wrote:

> ..if we assume not to use any of those standard string
> library functions (strcmp, strlen etc) then can we accomplish ?

You have at least two obvious choices :-

1) write strend in its entirety from scratch.
    It's not going to be terribly difficult.
    This function probably has limited general application.

2) write strlen and strcmp first and then use them to write strend.
    strlen and strcmp will probably be more generally useful.

[toc] | [prev] | [next] | [standalone]


#7090

FromTim Rentsch <txr@alumni.caltech.edu>
Date2011-06-27 11:54 -0700
Message-ID<kfnk4c785qf.fsf@x-alumni2.alumni.caltech.edu>
In reply to#7031
arnuld <sunrise@invalid.address> writes:

>> On Sat, 25 Jun 2011 10:07:44 -0700, Tim Rentsch wrote:
>
>
>> An alternative offered for everyone's reading pleasure and/or amusement
>> (as each may individually determine).
>> 
>> 
>>  /* Return 1 if 'tail' occurs at the end of 'whole', or 0 if not. */
>> 
>>  int
>>  strend( const char *whole, const char *tail ){
>>     size_t w, t;
>> 
>>     return
>>        whole  &&  tail
>>        &&   (w = strlen( whole ))  >=  (t = strlen( tail )) &&   strcmp(
>>        whole+w -t, tail ) == 0
>>     ;
>>  }
>
>
> Interesting :)  , if we assume not to use any of those standard string 
> library functions (strcmp, strlen etc) then can we accomplish ?

Then the function would be written differently.  I wrote the
above predicated on the stated presumption of availability of
said functions.

[toc] | [prev] | [next] | [standalone]


#7033

FromMark Bluemel <mark_bluemel@pobox.com>
Date2011-06-27 09:07 +0100
Message-ID<iu9dn8$onq$1@dont-email.me>
In reply to#6932
On 06/25/2011 06:07 PM, Tim Rentsch wrote:

> An alternative offered for everyone's reading pleasure
> and/or amusement (as each may individually determine).
>
>
>   /* Return 1 if 'tail' occurs at the end of 'whole', or 0 if not. */
>
>   int
>   strend( const char *whole, const char *tail ){
>      size_t w, t;
>
>      return
>         whole&&   tail
>         &&    (w = strlen( whole ))>=  (t = strlen( tail ))
>         &&    strcmp( whole+w -t, tail ) == 0
>      ;
>   }

Now if you'd used 'lo1' for "whole", "l0l" for "tail", "lol" for "w" and 
"l01" for "t", it would have been even more perfect...

[toc] | [prev] | [next] | [standalone]


#7089

FromTim Rentsch <txr@alumni.caltech.edu>
Date2011-06-27 11:51 -0700
Message-ID<kfnoc1j85vd.fsf@x-alumni2.alumni.caltech.edu>
In reply to#7033
Mark Bluemel <mark_bluemel@pobox.com> writes:

> On 06/25/2011 06:07 PM, Tim Rentsch wrote:
>
>> An alternative offered for everyone's reading pleasure
>> and/or amusement (as each may individually determine).
>>
>>
>>   /* Return 1 if 'tail' occurs at the end of 'whole', or 0 if not. */
>>
>>   int
>>   strend( const char *whole, const char *tail ){
>>      size_t w, t;
>>
>>      return
>>         whole&&   tail
>>         &&    (w = strlen( whole ))>=  (t = strlen( tail ))
>>         &&    strcmp( whole+w -t, tail ) == 0
>>      ;
>>   }
>
> Now if you'd used 'lo1' for "whole", "l0l" for "tail", "lol" for "w"
> and "l01" for "t", it would have been even more perfect...

Personally I think the single character identifiers do just fine
here, considering the length of the function, and their obvious
relationship to identifiers 'whole' and 'tail'.  But if you think
longer variable names would be helpful, by all means feel free.

If there are some other reservations or objections, please do
the group a favor and try to articulate what they are and why.

[toc] | [prev] | [next] | [standalone]


#7125

FromMark Bluemel <mark_bluemel@pobox.com>
Date2011-06-28 08:19 +0100
Message-ID<iubv9c$42i$1@dont-email.me>
In reply to#7089
On 06/27/2011 07:51 PM, Tim Rentsch wrote:
> Mark Bluemel<mark_bluemel@pobox.com>  writes:
>
>> On 06/25/2011 06:07 PM, Tim Rentsch wrote:
>>
>>> An alternative offered for everyone's reading pleasure
>>> and/or amusement (as each may individually determine).
>>>
>>>
>>>    /* Return 1 if 'tail' occurs at the end of 'whole', or 0 if not. */
>>>
>>>    int
>>>    strend( const char *whole, const char *tail ){
>>>       size_t w, t;
>>>
>>>       return
>>>          whole&&    tail
>>>          &&     (w = strlen( whole ))>=  (t = strlen( tail ))
>>>          &&     strcmp( whole+w -t, tail ) == 0
>>>       ;
>>>    }
>>
>> Now if you'd used 'lo1' for "whole", "l0l" for "tail", "lol" for "w"
>> and "l01" for "t", it would have been even more perfect...
>
> Personally I think the single character identifiers do just fine
> here, considering the length of the function, and their obvious
> relationship to identifiers 'whole' and 'tail'.  But if you think
> longer variable names would be helpful, by all means feel free.
>
> If there are some other reservations or objections, please do
> the group a favor and try to articulate what they are and why.

No objections or reservations. My posting was decidedly facetious.

I very much like your solution, but it's terseness somehow made me think 
of the IOCC and hence led me to suggest some obvious obfuscations.

[toc] | [prev] | [next] | [standalone]


#7168

FromTim Rentsch <txr@alumni.caltech.edu>
Date2011-06-28 16:03 -0700
Message-ID<kfnvcvp8son.fsf@x-alumni2.alumni.caltech.edu>
In reply to#7125
Mark Bluemel <mark_bluemel@pobox.com> writes:

> On 06/27/2011 07:51 PM, Tim Rentsch wrote:
>> Mark Bluemel<mark_bluemel@pobox.com>  writes:
>>
>>> On 06/25/2011 06:07 PM, Tim Rentsch wrote:
>>>
>>>> An alternative offered for everyone's reading pleasure
>>>> and/or amusement (as each may individually determine).
>>>>
>>>>
>>>>    /* Return 1 if 'tail' occurs at the end of 'whole', or 0 if not. */
>>>>
>>>>    int
>>>>    strend( const char *whole, const char *tail ){
>>>>       size_t w, t;
>>>>
>>>>       return
>>>>          whole&&    tail
>>>>          &&     (w = strlen( whole ))>=  (t = strlen( tail ))
>>>>          &&     strcmp( whole+w -t, tail ) == 0
>>>>       ;
>>>>    }
>>>
>>> Now if you'd used 'lo1' for "whole", "l0l" for "tail", "lol" for "w"
>>> and "l01" for "t", it would have been even more perfect...
>>
>> Personally I think the single character identifiers do just fine
>> here, considering the length of the function, and their obvious
>> relationship to identifiers 'whole' and 'tail'.  But if you think
>> longer variable names would be helpful, by all means feel free.
>>
>> If there are some other reservations or objections, please do
>> the group a favor and try to articulate what they are and why.
>
> No objections or reservations. My posting was decidedly facetious.

Oh okay.  I missed the intended tone then.

> I very much like your solution, but it's terseness somehow made me
> think of the IOCC and hence led me to suggest some obvious
> obfuscations.

I know some people think code being terse automatically makes
it obfuscated, but I don't share that opinion.  Writing code
compactly sometimes gives good code and sometimes bad, depending
on the particulars.  I do think that some aesthetic judgment
is required:  choices that work well in short functions often
fall down in longer functions, and vice versa.  Being exposed
to different styles is IMO a benefit, and that's a large part
of why I posted this alternative.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web