InterviewSolution
Saved Bookmarks
| 1. |
Check if a given string is a palindrome in Java |
|
Answer» A string is said to be PALINDROME if it is read the same forwards and backwards. An example of this is given as follows: String = madam The above string is palindrome.A program that checks if a string is palindrome in JAVA is given as follows: public class Example { public static void main(String args[]) { String str = "madam"; String strrev = ""; int N = str.length(); System.out.println("String: " + str); for(int i = n - 1; i >= 0; i--) { strrev = strrev + str.charAt(i); } if(str.equalsIgnoreCase(strrev)) { System.out.println("The above string is palindrome."); } else { System.out.println("The above string is not palindrome."); } } }The OUTPUT of the above program is as follows: String: madam The above string is palindrome. |
|