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");

}}

Recommended Answers

All 5 Replies

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));

CharArt is used for array ??

no
String text = "abcde";
char c = text.charAt(0); -> c = 'a';

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;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.