[RESOLVED] Checking Strings for Palintrome
Results 1 to 7 of 7

Thread: [RESOLVED] Checking Strings for Palintrome

  1. #1
    Junior Member
    Join Date
    Mar 2017
    Posts
    3

    resolved [RESOLVED] Checking Strings for Palintrome

    Hello,

    For my current project, I am trying to figure out how to check to see if a string typed into the text area is a palindrome. For this project, we are given most of the code, but I am supposed to fill in the code for the is_palindrome function that is given to me. I am not allowed to use the strrev function, recursion, or regular expressions and I am not allowed to change any other other code. I spent a few good hours trying to figure this out and after some research and trying to figure out it on my own, I got it to the point were it tells me if it is a palindrome or not, but I think the way I did it is recursion. I was never taught how to do recursion code, but after looking online what it is, I think my function has recursion. My questions for you guys are, is the code in the is_palindrome function recursion code, and if so, how could I put a loop (for or while) into that function that includes the strlen() and substr() function without making it recursion code?

    Thanks so much.

    PHP Code:
    <?php
    //declare variables to store the string to be checked and the message to be displayed.
    $string $message "";

    //retrieve the string to be checked and then strip any invalid characters
    if (filter_has_var(INPUT_GET"string")) {
        
    $string trim(filter_input(INPUT_GET'string'FILTER_DEFAULT));

        
    //remove all non-alphanumeric characters and then convert all characcters to lower cases
        
    $string_stripped strtolower(preg_replace("/[^A-Za-z0-9]/"""$string));
    }

    if (
    $string != "") {
        
    $result is_palindrome($string_stripped);

        if (
    $result)
            
    $message "\"" $string "\" is a palindrome.";
        else
            
    $message "\"" $string "\" is NOT a palindrome.";
    }



    //this is the  function that I am needed to fill out

    function is_palindrome($str) {

            if (
    strlen($str) == 0) {
                return 
    true;
            }
            if (
    $str[0] == $str[strlen($str) - 1]) {
                return 
    true and is_palindrome(substr($str1strlen($str) - 2));
            } else {
                return 
    false;
            }
        }
    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Checking Palindromes</title>
            <link rel="stylesheet" href="styles.css" type="text/css" />
        </head>
        <body>
            <h2>Checking Palindromes</h2>
            <p>Enter a string and then click the <strong>Check</strong> button to see the result.</p>
            <form action="palindrome.php" method="get" enctype="text/plain">
                <table>
                    <tr>
                        <th>Enter a string: </th>
                        <td><input type="text" name="string" size="20" value="<?php echo $string ?>" required /></td>
                    </tr>
                    <tr>
                        <th>Result: </th>
                        <td><?php echo $message ?></td>
                    </tr>
                </table>
                <br>
                <input type="submit" value="Check" />
            </form>
        </body>
    </html>

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,806
    My first thought is:
    PHP Code:
    function is_palindrome($text)
    {
      
    $justWordCharsstrtolower(preg_replace('/\W/'''$text));
      return 
    $justWordChars == strrev($justWordChars;

    (Untested)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Junior Member
    Join Date
    Mar 2017
    Posts
    3
    That might work, but... I am not allowed to use the strrev function as well, so your code would not work for my project. :/

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,806
    Since PHP strings can use array-style notation to access any one character, you could maybe do something like:
    PHP Code:
    function is_palindrome($text)
    {
      
    // any validation, stripping of white-space if desired, etc., then...
      
    $len strlen($text);
      for(
    $a=0$b=$len-1$a++, $b--; $a<$len)
      {
        if(
    $text[$a] != $text[$b]) {
          return 
    false;
        }
      }
      return 
    true;

    (also untested)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Junior Member
    Join Date
    Mar 2017
    Posts
    3

    resolved

    I just tested it and it works. I was not aware that you could have 2 different expressions and counter updates in the for loop. I learned something there.

    Thank you so much!

  6. #6
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,806
    Yeah, the multiple expressions like that in for() is very useful at times -- and most of us weren't aware it existed at first.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  7. #7
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    2,261
    Quote Originally Posted by NogDog View Post
    Yeah, the multiple expressions like that in for() is very useful at times -- and most of us weren't aware it existed at first.
    Honestly, it doesn't look odd or out of place when I see it. Yet everytime I see it I think "that's right, you can do that" lol
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •