Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > K&R exercise 4-1

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
 
 
 
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;
}


 
Reply With Quote
 
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 !!!!!!!!!

 
Reply With Quote
 
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tree functions daily exercise: Range Xah Lee Java 12 06-22-2005 08:51 AM
Cisco Student VPN exercise problem : gen_unrfrag: fail to generate unreachable, unexpected args robert Cisco 0 06-02-2004 07:33 PM
2154 module 4 Exercise 2 Drew Brown MCSE 0 10-22-2003 02:47 AM
Exercise needed for java 2 programmer test lonelyplanet999 Java 1 09-30-2003 10:37 AM
Re: Development best practices and knowing when to exercise control over development Kevin Spencer ASP .Net 2 08-06-2003 09:33 PM



Advertisments