aspose file tools*
The moose likes Programming Diversions and the fly likes finding half palindrome words Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "finding half palindrome words" Watch "finding half palindrome words" New topic
Author

finding half palindrome words

justin marshall
Greenhorn

Joined: Oct 08, 2003
Posts: 5
Hi all, I'm a newbie.
I managed to get a code that checks for palindrome words or strings.
A palindrome is a word that reads the same forwards and backwards. Like "redevider".
Now...How do I check for a HALF palindrome word or string? for example, "redivadar" is half palindrome because 3 out of 5 characters match. "redivaaar" is not even half palindrome because only 2 out of 5 chars match.
below is the code that checks for FULL palindromes.
Any ideas on how to tweak the code to check for HALF palindromes?

Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
I'm going to move this to Programming Diversions.
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
Hi El,
Try this
/*
* Palindrome.java
*
* Created on October 8, 2003, 2:27 PM
*/
/**
*
* @author Vinod K. Chandana
*/
public class Palindrome
{
public static void main (String[ ] args)
{
String inputSentence;
inputSentence = "redivadar";
int matches = 0;
int length = inputSentence.length() - 1;
int mid = (length + 1)/2;
for(int i = 0; i < mid; i++){
if(inputSentence.charAt(i) == inputSentence.charAt(length - i))
matches++;
}
if(matches < (mid/2))
System.out.println (inputSentence + " is NOT a palindrome.");
else if(matches == mid)
System.out.println (inputSentence + " is a palindrome.");
else if(matches == (mid/2))
System.out.println (inputSentence + " is a half palindrome.");
}
}
Regards,
Vinod
Originally posted by el chupacabra:
Hi all, I'm a newbie.
I managed to get a code that checks for palindrome words or strings.
A palindrome is a word that reads the same forwards and backwards. Like "redevider".
Now...How do I check for a HALF palindrome word or string? for example, "redivadar" is half palindrome because 3 out of 5 characters match. "redivaaar" is not even half palindrome because only 2 out of 5 chars match.
below is the code that checks for FULL palindromes.
Any ideas on how to tweak the code to check for HALF palindromes?

justin marshall
Greenhorn

Joined: Oct 08, 2003
Posts: 5
thank you thank you thank you.
I got the concept and the sample code is great!!
el chupacabra.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1752
    
    2
Great nickname, El Chupacabra!
But please adjust your displayed name to meet the JavaRanch Naming Policy. You can do so here.
Have fun on the 'Ranch. (We got some cattle and moose round these parts, but not too many goats.)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Just curious, does that algorithm work equally well for palindromes with even number letters an odd number letters? BOB and BOOB? Would left>=right instead of left>right end differently?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: finding half palindrome words