Write a program to accept any word and check whether the word is palindrome or not. Without storing the reverse word in another variable
Q Write a program to accept any word and check whether the word is palindrome or not. Without storing the reverse word in another variable.
SOLUTION:
import java.lang.String;
import java.util.Scanner;
class q3
{
public static void main(String args[])
{
String str;
int i = 0,j;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Word: ");
str = sc.nextLine();
j = str.length() - 1;
while(i < str.length() / 2)
{
if(str.charAt(i) != str.charAt(j))
{
System.out.println("Not a Palindrome.");
System.exit(0);
}
++i;
--j;
}
System.out.println("A Palindrome.");
}
}
Comments
Post a Comment