Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: K&R2, exercise 5-4, strend(s,t) |
| Date | 2011-06-23 23:03 +1200 |
| Message-ID | <96gktbFmo4U2@mid.individual.net> (permalink) |
| References | <4e031bce$0$312$14726298@news.sunsite.dk> |
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
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