Spread the love

Palindrome How it coded it

Spread the love

Palindrome in Java
Palindrome in Java

In this post I am going to publish an algorithm and corresponding code to implement a palindrome program.I have done this coding in HTML and java script. But if you understand the logic, you can easily implement in other language.
Well what can be an example of a palindrome: AABAA ,XXXX

logic to determine a word or a string as a palindrome is to determine the middle of the string and check the character of the first position with last position. if all the combination  matches till the middle of the string,We can say this is a valid palindrome.

Say
Case-1
AABAA—The length of the string is 5.Hence the middle is 5/2=2.5. 2.5 is not a valid position of a string.
Hence we will accept 3 is the middle position.
Case-2
XXXX—The length of the string is 4.Hence the middle is 4/2=2.

So after taking the input we need to determine if the string will be treated as Case-1 or Case-2.

Code—

          function palindrome()
               {
              //you can define your own string here.
               a=document.getElementById(“text”);
               var b=a.value;
               if(b.length%2) {
              // This is for your reference only
              document.getElementById(“test”).innerHTML=”odd”;
              doforodd(b,b.length);
               }
              else
              document.getElementById(“test”).innerHTML=”even”;
              doforeven(b,b.length);
             }


in my code the odd length is handled by  doforodd(b,b.length)  and the even is handled by doforeven(b,b.length)


Now how this even and odd  case is handled??


Evencase

function doforeven(b,length)
            {
                    mid=(length/ 2);
                    i=0;
                    j=length-1;
                    for (i=0;i<mid;i++){
                    if(b.charAt(i)==b.charAt(j))
                    {
                    doprint(“Y”);
                     }
                    else {
                    doprint(“N”);
                    }
                    j–;
                    }
                    Checkpal();
             }
                    


Here is the function for working with odd cases

function doforodd(b,length) {
                //find middle
                mid=((length-1)/ 2)+1
                mid”+mid;
                i=0;
                j=length-1;
                for (i=0;i<mid;i++){
                if(b.charAt(i)==b.charAt(j))
                {
                  doprint(“Y”);
                }
                else {
                    doprint(“N”);
                }
                j–;
                }
            Checkpal();
            
            }

doprint(“Y”)  function is only keeping track of the character matching. If the characters are matching it is going to print Yes or else it will print Not

function doprint(Var) {
             //you can create a string also and append the value    say output       
 document.getElementById(“test”).innerHTML=document.getElementById(“test”).innerHTML+Var;
                    if(Var==”Y”)
                    {
                        document.getElementById(“test”).innerHTML=document.getElementById(“test”).innerHTML+”Yes”;
                    }
                    else
                        document.getElementById(“test”).innerHTML=document.getElementById(“test”).innerHTML+”Not”;
                    }


Now Based on the string generated we need to determine if the given string is a palindrome or not.
How??
if the string (output) is having all yes then it is a true palindrome or else if you get atleast one “NOT” then it is not a valid palindrome.

READ  Oacle Openscript a new automation tool for automation
READ  Django-New web development framework

function Checkpal() {
                if (document.getElementById(“test”).innerHTML.indexOf(“Not”)>=0) {
                
                    document.getElementById(“test”).innerHTML=”Not a palindrom”;
                }
                else
                document.getElementById(“test”).innerHTML=”Palindrom”;
            }


You can use output.indexOf(“NOT”) also for your programming.


This program will give you if a given string is a palindrome or not.


If you want to test in webpage please copy paste the below written code in notepad and save it as “xyz.html”

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<script language=”javaScript”>
            function palindrome()
            {
            a=document.getElementById(“text”);
            var b=a.value;
            if(b.length%2) {
            document.getElementById(“test”).innerHTML=”odd”;
            doforodd(b,b.length);
              }
            else
            document.getElementById(“test”).innerHTML=”even”;
            doforeven(b,b.length);
            }
                function doforeven(b,length) {
                    mid=(length/ 2);
                   // document.getElementById(“test”).innerHTML=document.getElementById(“test”).innerHTML+”–mid”+mid;
                    i=0;
                    j=length-1;
                    for (i=0;i<mid;i++){
                    if(b.charAt(i)==b.charAt(j))
                    {
                    doprint(“Y”);
                    //document.getElementById(“test”).innerHTML=document.getElementById(“test”).innerHTML+”<br>,i=”+b.charAt(i)+”,j=”+b.charAt(j);
                    }
                    else {
                    doprint(“N”);
                    }
                    j–;
                    }
                    Checkpal();
                    }
                    
            function Checkpal() {
                if (document.getElementById(“test”).innerHTML.indexOf(“Not”)>=0) {
                
                    document.getElementById(“test”).innerHTML=”Not a palindrom”;
                }
                else
                document.getElementById(“test”).innerHTML=”Palindrom”;
            }
    
            function doprint(Var1) {
            document.getElementById(“test”).innerHTML=document.getElementById(“test”).innerHTML+Var1;
                    if(Var1==”Y”)
                    {
                        document.getElementById(“test”).innerHTML=document.getElementById(“test”).innerHTML+”Yes”;
                    }
                    else
                        document.getElementById(“test”).innerHTML=document.getElementById(“test”).innerHTML+”Not”;
                    }
            function doforodd(b,length) {
                //find middle
                mid=((length-1)/ 2)+1
                //document.getElementById(“test”).innerHTML=document.getElementById(“test”).innerHTML+”–mid”+mid;
                i=0;
                j=length-1;
                for (i=0;i<mid;i++){
                if(b.charAt(i)==b.charAt(j))
                {
                
               doprint(“Y”);
                }
                else {
                    doprint(“N”);
                }
                j–;
                }
            Checkpal();
            
            }
</script>
</head>
<body>
<form>
<input type=”text” id=”text” name=”tex”>
<input type=”button” value=”Click me” onclick=”palindrome()”>
<span id=”test”></span>
</form>
</body>
</html>

 

The output will be like the below written

This problem also solves the problem of odd even checking.

var b=”your String”
            if(b.length%2) {
            //this is odd
                   }
            else
            {
             //this is for even
                }

READ  Oacle Openscript a new automation tool for automation




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

A Little France in India Pondicherry Travel Plan

This is my experience and handpicked travel spots to make a...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Learn Quickly About DevOps Tools in a Fast and Efficient Manner

Introduction to DevOps tools DevOps tools are a collection of...

How to Collaborate between Development and Operations Team Fast and Effectively

Collaborate between Development and Operations Teams are the key...

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis...

EDITOR PICKS


Spread the love