Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Phil Carmody <thefatphil_demunged@yahoo.co.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: K&R2, exercise 5-4, strend(s,t) |
| Date | 2011-06-24 02:22 +0300 |
| Organization | A noiseless patient Spider |
| Message-ID | <87oc1o875q.fsf@bazspaz.fatphil.org> (permalink) |
| References | <4e031bce$0$312$14726298@news.sunsite.dk> <itvds4$nr9$1@dont-email.me> |
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
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