A palindrome, such as madam or racecar, is a word, number, phrase, or other sequence of letters that reads the same backward as forward. The following are some examples of when palindrome is used:
- Get the number from user
- Hold the number in temporary variable
- Reverse the number
- Compare the temporary number with reversed number
- If both numbers are same, print palindrome number
- Else print not palindrome number
Method 01
Explanation :
Here we will be using a simple logic to check palindrome in easy steps. STEP 01 get the string and reverse it, then STEP 02 compare the original String and Reverse string values if they are equal return true(palindrome) else return false(palindrome).
Code :
public class Palindrome {
public static String palindromeCheck(String n) {
/* return variable */
String ans = "";
/* variable to store reverse string */
String temp = "";
/* for loop to reverse the string */
for (int i = n.length() - 1; i >= 0; i--) {
/* concatenate character in string */
temp += n.charAt(i);
}
/* compares two strings return true is they are equal */
if (n.equals(temp)) {
ans = "Palindrome";
} else {
ans = "Not Palindrome";
}
/* Return the result*/
return ans;
}
public static void main(String[] args) {
/* declare the string to check palindrome */
String name = "dfhh";
/* call the palindromeCheck method */
System.out.println(palindromeCheck(name));
}
}
Output :
Not Palindrome
Steps :
- Define and declare a string variable in the main method.
- Call the user defined method in the print statement.
- Define a variable for return value.
- Define a temp variable to store reverse string.
- Using For loop to reserve the string.
- Use charAt() function to retrieve value and concatenate string value the in temp variable.
- Use equals() function to compares two strings return true is they are equal.
- Return the result.
Method 02
Explanation :
This a complex code but efficient, we will be comparing the character in the front value(i) of the string to rear value(j) of the string as mentioned in the below image. We will iterate the loop half the time of the length of the string. In the iteration when comparison is done front value will be incremented and rear value will be decremented. Example if the length of the string is 6 the loop will be iterate for 3 time, using the equality operator "==" we will compare primitives then if all the condition is true then it will true return.
Code :
public class Palindrome {
public static boolean palindromeCheck(String n) {
/* Declare variable for front */
int i = 0;
/* Declare variable for rear */
int j = n.length() - 1;
/* Declare variable for middle */
int k = (i + j) / 2;
/* loop to iterate half the length of the string */
for (int l = 0; l <= k; l++) {
/* condition to compare primitives */
if (n.charAt(i) == n.charAt(j)) {
/* increase value the front */
i++;
/* decrease the value of rear */
j--;
} else {
/* Return the result */
return false;
}
}
/* Return the result */
return true;
}
public static void main(String[] args) {
/* declare the string to check palindrome */
String name = "MADAM";
/* call the palindromeCheck method */
System.out.println(palindromeCheck(name));
}
}
Output :
true
Steps :
- Define and declare a string variable in the main method.
- Call the user defined method in the print statement.
- Define a variable for front value.
- Define a variable for rear value.
- Define a variable for middle value.
- Run for loop half the value of string.
- Compare primitives of string from front value and rear value using "==" operator.
- Increase front value and decrease rear value in iteration.
- Return the result.
Special Note: Palindrome is commonly asked in interview, method 01 is an easy implementation and method 02 is an efficient implementation. Hope this will help someone in future!! I welcome your feedback and solutions-oriented suggestions, thank you for your time.
Happy Coding❤️❤️❤️
Top comments (0)