String Program(Convert to palindrome words)
Write a program to accept a sentence and convert all the words in to palindrome words,the words that are already palindrome will remain same. Palindrome words are spelled same from both sides. Mom, dad, Malayalam are few examples of palindrome words.
Write a program to accept a sentence and convert all the words in to palindrome words,the words that are already palindrome will remain same. Palindrome words are spelled same from both sides. Mom, dad, Malayalam are few examples of palindrome words.
import java.util.*;
class MakePalin
{
public static String toPalin(String s)//to convert a word in to palindrome word and return
{
String temp="";
int len= s.length();
for (int i=len-1;i>=0;i--)
{
char ch= s.charAt(i);
temp+=ch;
}
if (temp.equals(s))//if the word is palindrome return as it is
return s;
else
{
String temp1="";
for (int i=len-2;i>=0;i--)
{
char ch= s.charAt(i);
temp1+=ch;
}
s+=temp1;
return s;
}
}
public static void main()
{
Scanner sc= new Scanner (System.in);
System.out.println("Enter a sentence");
String str= sc.nextLine();
StringTokenizer stz= new StringTokenizer(str);
System.out.println("The sentence with all the palindromic words is");
while (stz.hasMoreTokens()) //To extract the words
{
String tmp= stz.nextToken();
System.out.print(toPalin(tmp)+ " ");
}
}
}Output:
Enter a sentence
The water level is rising
The sentence with all the palindromic words is
ThehT wateretaw level isi risingnisir
Related Posts
Continue learning with these related articles

ISC Practical 2027: College Enrolment Code Validator and Stream Report – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 question: validate college enrolment codes made of a stream, year and serial number, give the exact reason for each rejection, auto-correct lowercase streams and prepare a stream-wise report.

ISC Practical 2027: Digital Lock Security Code Analyzer – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 number-logic question: for a 5 to 10 digit security code, find the first and last digits, odd and even position sums, product of non-zero digits, digit frequency and classify the code as STRONG, MODERATE or WEAK.

Boolean Algebra for ISC Class 12: Laws, Canonical SOP and POS, and Simplification
ISC Class 12 Computer Science notes on Boolean algebra: all the laws and theorems, duality, De Morgan's laws, minterms and maxterms, converting to canonical SOP and POS, and step-by-step algebraic simplification with solved examples.