0% found this document useful (0 votes)
3 views4 pages

Import Java

The document contains a Java program that identifies palindromic words in a user-input sentence. It defines a method to check if a word is a palindrome by cleaning the input and comparing characters. The program counts and displays the palindromic words found and their total number.

Uploaded by

saumalyonandi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Import Java

The document contains a Java program that identifies palindromic words in a user-input sentence. It defines a method to check if a word is a palindrome by cleaning the input and comparing characters. The program counts and displays the palindromic words found and their total number.

Uploaded by

saumalyonandi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

Scanner;

public class palindromicWords {

public static boolean isPalindrome(String word) {


String cleanedWord = [Link]("[^a-zA-Z]", "").toLowerCase();
int length = [Link]();
for (int i = 0; i < length / 2; i++) {
if ([Link](i) != [Link](length - 1 - i)) {
return false;
}
}
return true;
}

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

[Link]("Enter a sentence:");
String sentence = [Link]();

int palindromicWordCount = 0;
String[] palindromicWords=new String[20];
int palCount=0;
String tmp="";
for(int i=0;i<[Link]();i++)
{
if([Link](i)!=' '){tmp+=[Link](i); }
if(([Link](i)==' ' || i==[Link]()-1) &&
isPalindrome(tmp))
{
palindromicWords[palCount++]=tmp;
tmp="";
palindromicWordCount++;
}
if([Link](i)==' ')tmp="";

}
if(palCount > 0)
{
[Link]("\nPalindromic words found:");

for (int j=0;j<palCount;j++) {


[Link](palindromicWords[j]);
}
}

[Link]("\nTotal number of palindromic words: " +


palindromicWordCount);

}
}

You might also like