0
program for palindrome or not for a sentence string ?
3 Réponses
+ 1
import java.util.Scanner;
public class rev_num
{
public static void main(String[] args) {
System.out.println("enter the no");
Scanner sc1=new Scanner(System.in);
int num=sc1.nextInt();
System.out.println("the number is"+num );
int rev=0;
int t=num;
while (num>0)
{
int r=num%10;
rev=rev*10+r;
num=num/10;
}
num=t;
if(num==rev)
System.out.println(num+"is a pallindrome");
else
System.out.println(num+ "is not a pallindrome ");
System.out.println("reversed number:" +rev);
}
}
0
I have answer for number and not for string
0
Alter Shridhar's code a bit to accept a string rather than an integer and read individual characters. You may wanna add something in your loop to pass up spaces and punctuation, but something along the lines of an equality check that works towards the middle will work fine.