473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

K&R exercise 4-1

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

Aug 21 '06 #1
4 2780
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 '06 #2

"Aenima1891" <ae********@hotmail.it>
??????:11*********************@75g2000cwc.googlegr 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;
}
Aug 21 '06 #3

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

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

Aug 21 '06 #4
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 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: leonard greeff | last post by:
I want to know the correct way to answer exercise 1-11 of K&R. The only bug that I can find is that nw counts one to many words. (if there are 8 words, nw will be 9) Am I correct aor is there more...
46
by: Herrcho | last post by:
Hi~ i've studied C for a few months myself, and i'd appreciate it if anyone could improve my coding or correct it. the following is my solution to the K&R exercise 2-3 "Write the function...
12
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which...
12
by: Merrill & Michele | last post by:
It's very difficult to do an exercise with elementary tools. It took me about fifteen minutes to get exercise 1-7: #include <stdio.h> int main(int orange, char **apple) { int c; c=-5;...
8
by: Mike S | last post by:
Hi all, I noticed a very slight logic error in the solution to K&R Exercise 1-22 on the the CLC-Wiki, located at http://www.clc-wiki.net/wiki/KR2_Exercise_1-22 The exercise reads as...
16
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of...
19
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
5
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
9
by: JFS | last post by:
I know most of you have probably read "The C Programming Language" (K&R) at some point. Well here is something that is driving me crazy. The exercises are impossible (most of them) for me to do....
88
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.