K&R exercise 4-1

Discussion in 'C Programming' started by Aenima1891, Aug 21, 2006.

  1. Aenima1891

    Aenima1891 Guest

    Write the function strrindex(s,t) , which returns the position of the
    rightmost occurrence of t in s , or -1 if there is none.

    Here's my stuff, why doesn't work??!?


    #include <stdio.h>

    int strindex(char s[], char t[]);

    int main(void)
    {
    char s[]="mamma mia come sto";
    char t[]= "come";

    printf("%d", strindex(s, t));
    return 0;
    }

    int strindex(char s[], char t[])
    {
    int i, j, k;

    for(i= strlen(s)-1; i >= strlen(t)-1; i--) {
    for(j = i, k = strlen(t)-1; s[j] == t[k] && k>=0; j--, k--)
    ;
    if(k == 0)
    return i;
    }
    return -1;
    }
    Aenima1891, Aug 21, 2006
    #1
    1. Advertising

  2. Aenima1891

    Guest

    Aenima1891 wrote:

    > Write the function strrindex(s,t) , which returns the position of the
    > rightmost occurrence of t in s , or -1 if there is none.
    >
    > Here's my stuff, why doesn't work??!?
    >
    >
    > #include <stdio.h>
    >
    > int strindex(char s[], char t[]);
    >
    > int main(void)
    > {
    > char s[]="mamma mia come sto";
    > char t[]= "come";
    >
    > printf("%d", strindex(s, t));
    > return 0;
    > }
    >
    > int strindex(char s[], char t[])
    > {
    > int i, j, k;
    >
    > for(i= strlen(s)-1; i >= strlen(t)-1; i--) {
    > for(j = i, k = strlen(t)-1; s[j] == t[k] && k>=0; j--, k--)
    > ;
    > if(k == 0)
    > return i;
    > }
    > return -1;
    > }


    The way to investigate what's happening is to put
    printf statements at appropriate places in the strindex
    function to see what values the variables i,j,k take. Adding
    printf statements to observe the inner workings of a
    programme is a common debugging technique.

    In this case I'll give you a hint. Consider what happens in
    the loop for(j = i, k = strlen(t)-1; s[j] == t[k] && k>=0; j--, k--)
    if s[0] == t[0]

    A second hint is that you need to make 2 corrections as far
    as I can see. One should be obvious after the comment in
    the previous paragraph , the second somewhat less obvious.

    Spiros Bousbouras
    , Aug 21, 2006
    #2
    1. Advertising

  3. "Aenima1891" <>
    ??????:...
    > Write the function strrindex(s,t) , which returns the position of the
    > rightmost occurrence of t in s , or -1 if there is none.
    >
    > Here's my stuff, why doesn't work??!?
    >
    >
    > #include <stdio.h>
    >
    > int strindex(char s[], char t[]);
    >
    > int main(void)
    > {
    > char s[]="mamma mia come sto";
    > char t[]= "come";
    >
    > printf("%d", strindex(s, t));
    > return 0;
    > }
    >
    > int strindex(char s[], char t[])
    > {
    > int i, j, k;
    >
    > for(i= strlen(s)-1; i >= strlen(t)-1; i--) {


    > for(j = i, k = strlen(t)-1; s[j] == t[k] && k>=0; j--, k--)
    > ;
    > if(k == 0)
    > return i;
    > }
    > return -1;
    > }
    >


    I rewrite your code but I also do not get why your 2nd for() is wrong.

    int strindex(char s[], char t[]) {
    int i, j, k;

    for(i= strlen(s); i >= strlen(t); i--){
    // printf("For1:%d\t",i);
    for(j = i, k =strlen(t);(k>=0); j--, k--) {
    // printf("For2:%d\t",i);
    if (s[j] == t[k] && (k == 0)) return i;
    }
    }
    return -1;
    }
    Chen shuSheng, Aug 21, 2006
    #3
  4. Aenima1891

    Aenima1891 Guest

    I changed the condition
    if(k == 0)
    with
    if (k < 0)
    and now it seems to work

    thanks to all !!!!!!!!!
    Aenima1891, Aug 21, 2006
    #4
  5. Aenima1891

    Guest

    Aenima1891 wrote:

    > I changed the condition
    > if(k == 0)
    > with
    > if (k < 0)
    > and now it seems to work
    >
    > thanks to all !!!!!!!!!


    That was indeed the first of the 2 corrections I mentioned
    in my previous post , the obvious one. But there is another
    one you need to make , again in the loop
    for(j = i, k = strlen(t)-1; s[j] == t[k] && k>=0; j--, k--)
    You have already realized that k may take the value -1
    so consider which expressions get evaluated when k is
    -1. You will see that things are not ok even though the
    programme seems to work.

    Spiros Bousbouras
    , Aug 21, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer
    Replies:
    2
    Views:
    437
    John Saunders
    Aug 6, 2003
  2. lonelyplanet999

    Exercise needed for java 2 programmer test

    lonelyplanet999, Sep 30, 2003, in forum: Java
    Replies:
    1
    Views:
    4,181
    VisionSet
    Sep 30, 2003
  3. Xah Lee
    Replies:
    12
    Views:
    607
    Duncan Booth
    Jun 22, 2005
  4. Bruce .J Sam
    Replies:
    0
    Views:
    1,945
    Bruce .J Sam
    Jun 16, 2005
  5. Aries
    Replies:
    7
    Views:
    403
    Aries
    May 3, 2006
Loading...

Share This Page