Practice 8

Main | Arrays | Practice 8 | Solution 8 | Associative Arrays | Practice 9 | Solution 9 | Regular Expressions | Practice 10 | Solution 10 | More Regular Expressions | Practice 11 | Solution 11

A palindrome is a word that is spelled the same both forwards and backwards. The word "racecar" is a palindrome. The word "linux" is not a palindrome. Write a function that determines if a word (i.e. a string) is a palindrome.

To complete this exercise you must:

  • Create a file in "public_html/php" called "palindrome.php".
  • Be sure that "palindrome.php" is viewable from the web.
  • Fill in "palindrome.php" with this incomplete code.

Hints:

  1. You only have to implement the function called palindrome(). There is an array of strings that the function is tested on, but you don't have to worry about editing that code.

  2. Read in the string as an array and traverse the array backwards.

  3. There is a slight detail about how PHP stores a string as characters in an array. When PHP4 stores strings in an array it appends a \0 character to indicate the end of the array. So if you try to traverse the array backwards this is the first character that you run into, but it shouldn't be considered when evaluating if a string is a palindrome. To remove the complications of this detail I have provided a line of code to compute the length of the string in the sample code. Use this line of code.

  4. If you run into trouble print every step of what you are doing to track down the bug.


jfulton [at] member.fsf.org
22 Aug 2013