Palindromic Words Program in Java and Python
Palindromic words program with algorithm, explanation, Java solution and simple Python solution for ISC students.
Question:
A palindrome is a word that may be read the same way in either direction. Accept a sentence in uppercase which is terminated by either '.', '?' or '!'. Each word of the sentence is separated by a single blank space.
Perform the following tasks: display the palindromic words in the sentence and display the count of palindromic words.
Examples of palindromic words are MADAM, ARORA and NOON.
Algorithm:
Step 1: Start.
Step 2: Accept the sentence from the user.
Step 3: Check that the sentence ends with a valid terminating punctuation mark.
Step 4: Remove the terminating punctuation mark.
Step 5: Split the remaining sentence into words.
Step 6: For each word, form its reverse character by character.
Step 7: Compare the reversed word with the original word.
Step 8: If both are equal, display the word and increase the count.
Step 9: After all words are checked, display the count or a message if no palindrome is found.
Step 10: For each extracted word, initialize an empty reverse string before scanning its characters.
Step 11: Use a character index loop from word length - 1 down to 0 and append each character to the reverse string.
Step 12: Stop.
Explanation:
The sentence is first checked for valid termination because the ISC question allows only full stop, question mark or exclamation mark. The punctuation mark is removed before words are processed.
Each word is checked separately. To test a word, the program forms a reverse copy by reading its characters from the last position to the first position.
If the reversed word is exactly equal to the original word, the word is palindromic. The program prints such words immediately and increases the counter.
The counter is important because the program must also display how many palindromic words were found. If the counter remains 0, the program displays that there are no palindromic words.
The word loop and the character loop have different roles. The outer processing extracts one word at a time, while the inner reverse-building loop compares characters from the end of that word back to the beginning.
The program identifies palindromic words inside a sentence. A word is palindromic if it reads the same from left to right and right to left. The sentence is first split into words, ignoring punctuation as required. For each word, the program either reverses the word and compares it with the original or compares characters from opposite ends. A counter records how many palindromic words are found. Case handling is important so that words are compared consistently.
Java Program:
/**
* The class Palin_ISC2013 inputs a sentence and prints all the Palindromic words in it
* along with it's frequency
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2013 Question 3
*/
import java.util.Scanner;
import java.util.*;
class Palin_ISC2013
{
static
Scanner sc = new Scanner(System.in);
boolean isPalin(String s)
{
int l=s.length();
String rev="";
for(int i=l-1; i>=0; i--)
{
rev=rev+s.charAt(i);
}
if(rev.equals(s))
return true;
else
return
false;
}
public static void main(String args[])
{
Palin_ISC2013 ob=new Palin_ISC2013();
System.out.print("Enter any sentence : ");
String s=sc.nextLine();
s=s.toUpperCase();
StringTokenizer str = new StringTokenizer(s,".?! ");
int w=str.countTokens();
String word[]=new String[w];
for(int i=0;i<w;i++)
{
word[i]=str.nextToken();
}
int count=0;
System.out.print("OUTPUT : ");
for(int i=0; i<w; i++)
{
if(ob.isPalin(word[i])==true)
{
count++;
System.out.print(word[i]+" ");
}
}
if(count==0)
System.out.println("No Palindrome Words");
else
System.out.println("\nNumber of Palindromic Words : "+count);
}
}Equivalent Python Program:
# Read the text input and split or scan it according to the question requirement.
# Loops process each word/character and update counters or result strings.
# Display the final text result after sorting, checking or rearranging is complete.
sentence = input("Enter a sentence: ")
last = sentence[len(sentence) - 1]
if last != '.' and last != '?' and last != '!':
print("INVALID INPUT")
else:
sentence = sentence[0:len(sentence) - 1]
words = sentence.split()
count = 0
answer = ""
for word in words:
rev = ""
for i in range(len(word) - 1, -1, -1):
rev = rev + word[i]
if rev == word:
answer = answer + word + " "
count = count + 1
if count == 0:
print("NO PALINDROMIC WORDS")
else:
print(answer.strip())
print("NUMBER OF PALINDROMIC WORDS:", count)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.