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?