# K&R exercise 4-1

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

1. ### Aenima1891Guest

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

2. ### 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

3. ### Chen shuShengGuest

"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
4. ### Aenima1891Guest

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

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

Aenima1891, Aug 21, 2006
5. ### 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