Question
print non-palindrome words in a given sentence.
A non-Palindrome words are words that are not same backward and forward
example:
pop reads same, backward or forward so it a Palindrome word
mom reads same, backward or forward so it a Palindrome word
cat does not spell same backward and forward so cat is an non-Palindrome word
Enter a sentence:Mom and dad are not at home
Non-Palindrome words in given sentence:
AND
ARE
NOT
AT
HOME
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class nonPalindromeWordsInSentence
{
public static void main(String[] args)
{
String sen="",wd="",wd1="";
char ch=' ';
int i=0,len=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence:");
sen = sc.nextLine();
sen=sen+" ";
len=sen.length();
System.out.println("Non-Palindrome words in given sentence:");
for(i=0;i< len;i++)
{
ch=sen.charAt(i);
if(ch==' ')
{
if(wd.equalsIgnoreCase(wd1)==false)
{
System.out.println(wd);
}
wd1="";
wd="";
}
else
{
wd=wd+ch;
wd1=ch+wd1;
}
}
}
}
Python
sentence=input("Enter a sentence:")
#making all the characters in the given sentence to upper case
#using upper()
sentence=sentence.upper()
#split(' ') will split the sentence whenever a space is found
#and creates a list
words=sentence.split(' ')
print("Non-Palindrome words in given sentence:")
for word in words:
reverseOfWord=""
for i in range(0,len(word)):
reverseOfWord=word[i]+reverseOfWord
if(word!=reverseOfWord):
print(word)