hi
I want to implement this program correctly
about plindrom like ( ara) is plindrom
i try to writed code for non-recursivly
but i didn't understand method CharAt ()
IgnorCase()

import java.util.Scanner;
public class Plindrom {
public static void main (String[] args)

{

    String str;
    int left, right;
    Scanner scan = new Scanner (System.in);
        System.out.println ("Enter string or sentence: ");
        str = scan.nextLine();
        left = 0;
        right = str.length() - 1;
      while (left < right) {       // continue until they reach center
      if (str.charAt(left) != str.charAt(right)) {
        System.out.println(" it is plindrom");
      }
      left++;               // move left index toward the center
      right--;             // move right index toward the center
   }

    System.out.println(" it is not plindrom");

}}

you start with a string
the charAt method will return the char on the index you provide in the string you apply it to.
IgnoreCase() => this can occur in equalsIgnoreCase(), a method that you can find in the String class.
just run the next piece of code, and you'll see immediately what it does:

String one = "original";
String two = "oRiGiNal";

System.out.println("equals = " + one.equals(two));
System.out.println("ignore = " + one.equalsIgnoreCase(two));

my code in above is always give me not plindrom even its plindrom
can you plz help me what is error there

you are printing is not a palindrome every time you compare two chars and they are equal.

I think it's better to write a method in which you test for the string to be a palindrome, and return a boolean telling your main method that it is (or not)

public static void main(String[] args){
if ( isPalindrome("text"))
  System.out.println("palindrome");
else
  System.out.println("not a palindrome");
}

private static boolean isPalindrome(String input){
boolean isPalindrome = true;
// check String and set isPalindrome to false if it's not a palindrome
// you'll be needing a loop here
return isPalindrome;

}

Edited 5 Years Ago by stultuske: n/a

This article has been dead for over six months. Start a new discussion instead.