Advertisement
zachgordon25

PalindromeMethods

Nov 17th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. package week11.solutions;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PalindromeMethods {
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.  
  9.         System.out.print("Enter a string: ");
  10.         String s = input.next();
  11.  
  12.         boolean result = isPalindrome(s);
  13.         System.out.println(s + (result ? " is a palindrome" : " is not a palindrome"));
  14.  
  15.     }
  16.  
  17.     static boolean isPalindrome(String word) {
  18.         int low = 0;
  19.         int high = word.length() - 1;
  20.  
  21.         while (low < high) {
  22.             if (word.charAt(low) != word.charAt(high)) {
  23.                 return false;
  24.             }
  25.            
  26.             low++;
  27.             high--;
  28.         }
  29.         return true;
  30.     }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement