Output

Variable Name      Value in Memory
low
high
isPalindrome
You have entered an empty string
0 1 2 3 4 5 6 7 8 9
a a a a a a a a a a
    
  1  import java.util.Scanner;
  2  
  3  public class Palindrome {
  4    /** Main method */
  5    public static void main(String[] args) {
  6      // Create a Scanner
  7      Scanner input = new Scanner(System.in);
  8  
  9      // Prompt the user to enter a string
 10      System.out.print("Enter a string: ");
 11      String s = input.nextLine();
 12  
 13      // The index of the first character in the string
 14      int low = 0;
 15  
 16      // The index of the last character in the string
 17      int high = s.length() - 1;
 18  
 19      boolean isPalindrome = true;
 20      while (low < high) {
 21        if (s.charAt(low) != s.charAt(high)) {
 22          isPalindrome = false;
 23          break;
 24        }
 25  
 26        low++;
 27        high--;
 28      }
 29  
 30      if (isPalindrome)
 31        System.out.println(s + " is a palindrome");
 32      else
 33        System.out.println(s + " is not a palindrome");
 34    }
 35  }
            
s
low
high