Janie Lee
Janie Lee

Reputation: 29

moving the logic for determining if a string is a palindrome

Currently incredibly confused about how I would go about moving the logic for determining if a string is a palindrome from the main method into a method named checkPalindrome? My method should have a single String argument and return a boolean value, but I'm still unsure of how to do this.

import java.util.Scanner;

public class PalindromeCheckMethod {

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a String: ");
        String s = input.nextLine();
        boolean isPalindrome = checkPalindrome(s);
        String msg = "not a palindrome";
        if (isPalindrome) {
            msg = "a palindrome";
        }
        System.out.printf("%s is %s.%n", s, msg);

        s = s.toLowerCase();
        String resultString = "";

        for (int i = 0; i < s.length(); i++) {
            if (Character.isLetter(s.charAt(i)))
                resultString += s.charAt(i);
        }
        s = resultString;

        int low = 0;
        int high = s.length() - 1;

        if (high >= 0)  {


        while (low < high) {
            if (s.charAt(low) != s.charAt(high)) {
                isPalindrome = false;
                break;
            }
                low++;
                high--;
            }
        }
        else {
            isPalindrome = false;
        }

        if (isPalindrome)
            System.out.println(s + " is a palindrome. ");
        else 
            System.out.println(s + " is not a palindrome. ");


        }

private static boolean checkPalindrome(String s) {
    return false;
}

    }

Upvotes: 1

Views: 87

Answers (3)

MAK
MAK

Reputation: 59

static boolean checkPalindrome(String str) 
{
   int strEndPoint=str.length()-1;
   int strStartPoint=0;
   boolean isPalindrome=true;
   while(strStartPoint<=strEndPoint)
   {
       if(str.charAt(strStartPoint)!=str.charAt(strEndPoint))
       {
          isPalindrome=false;
          break;
       }
     strStartPoint++;
     strEndPoint--;
  }
 return isPalindrome;

}

Upvotes: 1

Afsin Buyuksarac
Afsin Buyuksarac

Reputation: 298

Why dont you think simple. Precisely, mirror image of the chars would be same and if it is not it is not palindrome. When you cross middle of the array without finding a difference it is an absolute palindrome. Check this out.

int len = str.getLenght();
int i = 0;

do {
    if (str.get(i) == str.get(len-1-i))
        i++;
    else {
        print("NOT a palindrome");
        break;
    }

} while (i < len-1-i) ;

if (i < len-1-i)  print("NOT a palindrome");    

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201537

Well, I would use a StringBuilder and implement a method of that description with something like,

static boolean checkPalindrome(final String s) {
    StringBuilder sb = new StringBuilder(s);
    sb.reverse();
    return sb.toString().equalsIgnoreCase(s);
}

But you could certainly replace my implementation with your implementation (I did not validate your implementation). To call the method (and perhaps take advantage of formatted io) you could use something like

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a String: ");
    String s = input.nextLine();
    boolean isPalindrome = checkPalindrome(s);
    String msg = "not a palindrome";
    if (isPalindrome) {
        msg = "a palindrome";
    }
    System.out.printf("%s is %s.%n", s, msg);
}

Upvotes: 0

Related Questions