for loop - Verify the palindrome in Java. Can you help me to find what's the issue with this code -
can me find bug in code. when test string not palindrome, message palindrome.
import java.util.*; public class main{ public static void main(string args[]){ string input = ""; system.out.println("enter string verify palindrome"); scanner scan = new scanner(system.in); input = scan.nextline(); main m = new main(); if(m.palindrome(input)) system.out.println(" string " + input + " palindrome "); else system.out.println(" string " + input + " not palindrome "); } private boolean palindrome(string input){ string reverse = input; int j; for(int i=0;i<=reverse.length()-1;i++){ for( j=reverse.length()-1;j>=0;){ if(reverse.charat(i)== reverse.charat(j)){ return true; } else{ return false; } }j--; } return false; } }
if(reverse.charat(i)== reverse.charat(j)){ return true; }
you returning true if first , last character same without going on check other character.
i'd better approach continue stepping through word until find character not match or until finish. if find character not match, return false. if finish, return true.
Comments
Post a Comment