Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. public class Ex4 {
  2.     public static void main(String[] args) {
  3.  
  4.         String word = "kayak";
  5.  
  6.         char[] palindrome = new char[word.length()];
  7.  
  8.         for (int i = 0; i < word.length(); i++) {
  9.             palindrome[i] = word.charAt(i);
  10.  
  11.         }
  12.  
  13.         int isPalindrome = 0; //true - 0, false - 1
  14.         int sumPalindrome = 0;
  15.         for (int i = 0; i < word.length(); i++) {
  16.  
  17.             if (palindrome[i] == palindrome[word.length() - 1 - i]) {
  18.                 isPalindrome = 0;
  19.             } else {
  20.                 isPalindrome = 1;
  21.             }
  22.  
  23.             sumPalindrome += isPalindrome;
  24.         }
  25.        
  26.  
  27.         if (sumPalindrome == 0 && word.length() > 0) {
  28.             System.out.println("Palindrome.");
  29.         } else {
  30.             System.out.println("Not a palindrome.");
  31.         }
  32.  
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement