K&R exercise 4-1

A

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;
}
 
S

spibou

Aenima1891 said:
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
 
C

Chen shuSheng

Aenima1891 said:
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;
}
 
A

Aenima1891

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

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

spibou

Aenima1891 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top