Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: K&R2, exercise 5-4, strend(s,t) |
| Date | 2011-06-25 10:07 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <kfnoc1lalgf.fsf@x-alumni2.alumni.caltech.edu> (permalink) |
| References | <4e031bce$0$312$14726298@news.sunsite.dk> <itvds4$nr9$1@dont-email.me> |
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
;
}
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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