program for palindrome or not for a sentence string ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

program for palindrome or not for a sentence string ?

30th Aug 2016, 1:54 PM
pranay kalamkar
pranay kalamkar - avatar
3 Answers
+ 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); } }
30th Aug 2016, 6:10 PM
SHRIDHAR M PATIL
0
I have answer for number and not for string
30th Aug 2016, 6:09 PM
SHRIDHAR M PATIL
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.
1st Sep 2016, 6:23 AM
Andrew Pruitt
Andrew Pruitt - avatar