Re: [Tutor] strings

Magnus Lycka magnus at thinkware.se
Tue Dec 9 14:51:56 EST 2003


>      i'm having a little trouble with this exercise problem. We're supposed to create a function called findchr(string,char) which will look for character char in string and return the index of the first occurrence of char, or -1 if char is not part of string. you can't use string.*find() or string.*index() functions or methods. The part where i'm having trouble is returning the index of the first occurrence of char. Any help would be appreciated. Thanks

Hi there, we try not to spoil education on this list by doing
peoples school work. (It's so easy to just provide an answer,
but it's probably not very educational...)

This exercise is really so small that it's difficult to give 
a hint without solving it entirely. I guess you're either just 
stuck, or haven't still grasped how to combine the elements of 
a programming language to solve a problem. 

A good way of solving problems is usually to solve a little piece
at a time...

Could you write a program that can compare two values (numbers
or strings or whatever) to see if they are equal?

Could you write a program that would give the right answer just
in the case that your char is in the first position of the string?

Could you write a program that would give the right answer just
in the case that your char is in the second position of the string?

Can you think of any way of generalizing such a program?

If you read chapters 3 and 4 in the Python tutorial, you will
certainly have all the tools you need to do this. Just take one
step at a time. Good luck!

http://www.python.org/doc/current/tut/node5.html
http://www.python.org/doc/current/tut/node6.html

-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list