Problem statement:
How do you check palindrome?
How do you check palindrome?
- public class Palindrome {
- public static void main(String[] args) {
- String s = "madam";
- isPalindrome(s);
- }
- public static void isPalindrome(String s) {
- String rev = "";
- char c[] = s.toCharArray();
- for (int i = s.length() - 1; i >= 0; i--) {
- rev = rev + c[i];
- }
- if (s.equals(rev))
- System.out.println("Yes");
- else
- System.out.println("No");
- }
- }
Output:
Yes
Mehod-II:
Mehod-II:
- public class Palindrome {
- public static void main(String[] args) {
- String s = "madam";
- System.out.println(isPalindrome(s) ? "Yes" : "No");
- }
- public static boolean isPalindrome(String s) {
- int p = s.length() - 1;
- boolean flag = true;
- char c[] = s.toCharArray();
- for (int i = 0; i < s.length() / 2; i++) {
- if (c[i] != c[p]) {
- flag = false;
- break;
- }
- p--;
- }
- return flag;
- }
- }
Yes
No comments:
Post a Comment