[Tutor] string comparison

Wesley Chun wesc@deirdre.org
Wed, 11 Jul 2001 20:49:32 -0700 (PDT)


On Wed, 11 Jul 2001, Danny Yoo wrote:
> On Wed, 11 Jul 2001, Paul Brown wrote:
>
> > Core Python Programming, and Exercise 6-5(b) says i have to determine if
> > two strings match (without using the comparison operators or the cmp()
> > built-in function) by scanning each string.  Also, for extra credit, it
>
> Do you mean checking each character in the string, letter by letter?

well, character-by-character is more like it!!


> > asks me to add case-insensitivity to the solution.  does anyone have any
> > suggestions?  i can only think to use the "==" operator.  thanks
>
> Whoa!  That seems really restrictive to not allow '=='!  Does the exercise
> kebep us from doing '==' altogether, or just against two long strings?

well heck!  if you use '==', the exercise is over!!
of *course* it's restrictive!!  you gotta *make* strcmp()
before you can really appreciate it!  one char-at-a-time.


> (Wesley, I will buy your book today... *grin*)

i told you that i'd hand one to you if you help me out!!
i'll even sign it!!  *W*


> I can think a convoluted way of getting around the restriction of not
> using '=='.

the exercise is simple.  given 2 strings, determine if they are
"the same."  (no, i'm not talking about whether the objects are
the same, just the string contents.)

if str1 == 'foo' and str2 == 'foo', even if they are different
objects, the strings themselves are made up of the same characters.

if str1 == 'foo' and str2 == 'bar', they are *not* the same.

after this exercise is complete, upgrade it with case-insensitivity
so that if str1 == 'FoO' and str2 = 'fOo', then they match.

hope this helps!

-wesley

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Silicon Valley-SF Bay Area Python users group:  http://baypiggies.org

"Core Python Programming", Prentice Hall PTR, December 2000
    http://starship.python.net/crew/wesc/cpp/

wesley.j.chun :: wesc@baypiggies.org
cyberweb.consulting :: silicon.valley, ca
http://www.roadkill.com/~wesc/cyberweb/