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.
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 “
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) 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”(
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.
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.