Program for finding out PALINDROME of a string in Java(3 Methods)

Description


Description

In this program first and last charcters of string is compared one by one moving towards center .

Here string is entered in the variable ‘st’ of type String by user.

Now sring is converted into upper case and saved in new string variable ‘str’, this is done to ignore the conditions when the entered string is like ‘RAcecar’,after converting ,it becomes like ‘RACECAR’.

In for loop we take first and last characters of string and compare them,

if they are equal ‘if’ condition is not executed and looping countinuos by incrementing first and decrementing last variables

if they are not equal ‘if’ condition is executed and looping terminates through break statement and flag is assinged the vale 0

Now variable flag is compared for checking the Palindrome.

Description

In this program we are taking a string (original) whose characters are concatenated into a new string variable from the end of the original string.

So actually, we are taking a string (entered by the user) which is stored in variable org. Now we are taking another String variable rev which is initialised by a white space character (” “).

Now we have taken an integer variable length which is basically used for storing the length of the entered string (org string length). Now we are needed to convert the original string (org) into an Array of Characters with the help of a function “.toCharArray()”, which is stored into the array of characters named as stin the program. We’ve stored the length of array st into the variable named length (length=st.length) using length as a keyword in st.length.

From here the main logic starts… Here we have used a for loop having variable name – last. As we know if we enter a string (say LEVEL) then the keyword length which is applied on it will give us the value as 5. So, its clear that the variable length will store 5 in it. Here our loop is starting from length-1 due to the reason that array always starts from 0. Now, the condition of the loop will last upto 0 or greater than it. If you see the code the loop is has a decrement of 1.

So mainly the loop is starting from 4 & will not end until the value of the variable last will be greater or equal to zero[such as – for(last=4;last>=0;last–)]. As per the statement of the for loop (rev=rev+st[last]). This means that the last character of the array st(st[4]) will be concatenated into a string rev which has been already initialised by a white space character.

As per the above loop string rev should have the value – “ LEVEL”(LEVEL). So, as we can see here after comparing the variables rev & org they are not same/equal because of a whitespace character at the first place of the string rev. But LEVEL is a palindrome string, so to overcome this fault we should concatenate the string variable org with the whitespace character onto its first place, by using the statement org=” “+org.

Now, at last our work is to compare both the org & rev strings with each other by ignoring case sensitive letters using the function equalsIgnoreCase() as used in the program. After comparing if we found both the strings equal it means that the original(org) string is PALINDROME.

Description

In this program we use reverse() fuction to determine PALINDROME.

Here string is entered in the variable ‘org’ of type String by user.

Now a new string is declared ‘rev’ in which reversed string will be saved.

StringBuffer is class which contains the reverse() funtion for string.

The object of StringBuffer ‘ob’ is declared along with given string as argument.

The reverse() funtion is invoked and reverse string is saved in variable ‘rev’.

Here we have used toString() funtion since the StringBuffer class does not override the equals() funtion from the Object class,contents of string buffers should be converted to String objects for string comparison.

Now the variables ‘org’ and ‘rev’ are compared using equalsIgnoreCase() function and hence the result is printed.

SOURCE CODE
SOURCE CODE
SOURCE CODE

 

OUTPUT

OUTPUT

OUTPUT

Author Description

humanz

Our mission is to bring the affordable Technology education to people everywhere, in order to help them achieve their dreams and change the world. - Follow @program_guide

No comments yet.

Join the Conversation