Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6153515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:06:25+00:00 2026-05-23T20:06:25+00:00

I was working on (for fun) writing a script that would recognize palindromes. So

  • 0

I was working on (for fun) writing a script that would recognize palindromes. So far, I’m successful with “Kayak”, “Racecar”, “Anna”, “A man a plan a canal Panama”: yet variations on the latter phrase such as “amanaplana canalpan ama” gives me problems.

As a side note: I do understand that using PCRE would make things a lot easier for me, but I’m not fluent in it and one of my major aims was to understand the algorithm behind checking for palindromes.

 <?php
$word = "amanaplana canalpan ama";

$space = " ";
$word_smallcase = strtolower($word);        

$word_array = str_split($word_smallcase);   

if(in_array($space, $word_array)){      

    for($m = 0; $m<count($word_array); $m = $m + 1){
        if($word_array[$m] == $space)
            unset($word_array[$m]);
    }
}
$count = 0;
$scan_count = -1;
for($i = 0; $i < (count($word_array)/2); $i = $i + 1){

    for($j = count($word_array); $j > (count($word_array)/2); $j = $j - 1){

        if($word_array[$i]==$word_array[$j]){
            $count = $count + 1;
            break;
            }
        }
    $scan_count = $scan_count + 1;

        }
if ($count == $scan_count){
    echo $word." is a palindrome";
}
else{

    echo $word ." is NOT a palindrome";
}


?>

I’d appreciate a response regarding:

  • The identification of the bug I’m having.
  • Recommendations as to
    how I could possibly improve the code (I’d be happy if I could make
    things work without resorting to $count or $scan_count which seem, to
    my eye, relatively amateurish).

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:06:26+00:00Added an answer on May 23, 2026 at 8:06 pm

    There’s a few things going on here…

    First, I’m not sure if you’re aware that unset‘ing the array doesn’t actually remove the indices:

    $array = array(0, 1, 2, 3);
    unset($array[2]);
    var_dump($array);
    /* array(3) {
      [0]=>
      int(0)
      [1]=>
      int(1)
      [3]=>
      int(3)
    } */
    

    So you’re going to have some undefined offsets when you iterate over the elements in the array. To go one by one, you should use the foreach loop control.

    Another issue lies in the nested for loop here:

    for($i = 0; $i < (count($word_array)/2); $i = $i + 1){
    
        for($j = count($word_array); $j > (count($word_array)/2); $j = $j - 1){
    

    Given “amanaplanacanalpanama”, look at what you’re doing:

    comparing, step by step (btw, you’re off by 1 on the initializer for $j… $word_array[count($word_array)] is pointing at the ‘m’ in panama, not the ‘a’.):

    a eq to a?              j is 22          i is 0
                    scan_count: -1   count: 1
    m eq to a?              j is 22          i is 1
    m eq to m?              j is 21          i is 1
                    scan_count: 0    count: 2
    a eq to a?              j is 22          i is 2
                    scan_count: 1    count: 3
    

    a eq to a is fine, and matches… m is found on the next iteration, but when you get to the next ‘a’, you’re finding the original ‘a’ at the end of panama…

    As a side note, since you are starting over from the very end every time, it would be horribly inefficient O(n^2) given a sufficiently large string…

    Obligatory solution:

    $word = "amanaplana canalpan ama";
    
    $j = strlen ($word)-1;
    $pal = true;
    
    for ($i = 0; $i < strlen($word)/2; ++$i, --$j) {
    
        // skip spaces
        while ($word[$i] === " ") {$i++;}
        while ($word[$j] === " ") {$j--;}
    
        echo "$word[$i] eq $word[$j]?\n";
        if ($word[$i] !== $word[$j])    {
            $pal = false;
            break;
            }
    }
    
    if ($pal) print "yes"; else print "no";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently been working on a Pastebin script (for fun) and I've come
For the fun, I want to create a ( working ) executable that doesn't
I'm working on a small fun projects that builds a robot. We as the
I'm working on a fun project where I need a simple key/value store that
Yeah, this ought to be fun. I'm working on a site that was built
I'm currently working on a program (for fun, this is not an assignment) that
Hi I was working on a bit of fun, making an interface to run
So, I've been working on a little visualizer for sound files, just for fun.
Working with a SqlCommand in C# I've created a query that contains a IN
Working on a project that parses a log of events, and then updates a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.