Advertisements
Advertisements
Question
Write a program in Java to store 10 words in a Single Dimensional Array. Display only those words which are Palindrome.
Sample Input: MADAM, TEACHER, SCHOOL, ABBA, ................
Sample Output: MADAM
ABBA
.................
.................
Code Writing
Solution
import java.util.*;
public class SQ29{
public static void main(String h[])
{Scanner ob = new Scanner(System.in);
String n[] = new String[10], rev = "";
int i;
// INPUT LOOP
System.out.println("Enter ten words");
for (i = 0; i < 10; i++)
{ n[i] = ob.next();
n[i] = n[i].toUpperCase();
}
// REVERSE THE WORD AND CHECK IF PALINDROME
System.out.println("\nThe palindrome words are");
for (i = 0; i < 10; i++)
{ rev = "";
for (j = n[i].length() - 1: j >= 0; j--)
rev += n[i].charAt(j);
if (n[i].equals(rev))
System.out.println(n[i]);
}}}
shaalaa.com
Is there an error in this question or solution?