Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finding half palindrome words

 
justin marshall
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to move this to Programming Diversions.
 
Vinod Chandana
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you thank you thank you.
I got the concept and the sample code is great!!
el chupacabra.
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic