Velocity Reviews > K&R exercise 4-1

# K&R exercise 4-1

Aenima1891
Guest
Posts: n/a

 08-21-2006
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;
}

spibou@gmail.com
Guest
Posts: n/a

 08-21-2006
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

Chen shuSheng
Guest
Posts: n/a

 08-21-2006

"Aenima1891" <(E-Mail Removed)>
??????:(E-Mail Removed) oups.com...
> 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;
}

Aenima1891
Guest
Posts: n/a

 08-21-2006

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

thanks to all !!!!!!!!!

spibou@gmail.com
Guest
Posts: n/a

 08-21-2006
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