i have wrote java function reversing user input but it nor working and i don't have any idea where is the problem and how to solve it.
import java.util.Scanner;
class Reverse{
public static void main(String args[]){
Scanner word = new Scanner(System.in);
System.out.print("type the word here to check if it is palindrome: ");
String n = word.nextLine();
char[] let = n.toCharArray();
System.out.print(revrse(i, let));
// for (int i=let.length-1; i>=0; --i){
// System.out.print(let[i]);
// }
}
public static int reverse(char[] let){
for (int i=let.length-1; i>=0; --i){
return let[i];
}
}
}
how to make this code to work.
reversemethod simply returns the ordinal of the last character in the character array, it does not reverse it. As @Shail016 mentioned you could just usenew StringBuilder(n).reverse().toString().Stringis aPalindromeor not, why to reverse it. Simply take a variable, initialize it tosay i = 0and second one` initialize it tosay j = let.length - 1start the loop from0to< let.length / 2and comparelet[i] with let[j]